• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天下

            記錄修行的印記

            c++知識點:返回引用,前置聲明,using

            //1.h
            #pragma once
            //class std::string; 含有命名空間的話,這樣使用無效,
            using std::string;
            string& GetString(int n); 

            extern std::map<int,std::string> strings;

            //1.cpp
            #include "stdafx.h"
            std::map
            <int,std::string> strings;


            /*
            千萬不要返回局部對象的引用。當函數執行完畢時,將釋放分配給局部對象的存儲空間。此時,對局部對象的引用就會指向不確定的內存。如:

            const string &manip(const string &s)
            {
                string ret =s;
                return ret;  //wrong:returning reference to a local object
            }
            4,引用返回左值。返回引用的函數返回一個左值。因此這樣的函數可用于任何要求使用左值的地方。
            示例見:c++ primer p215

            */
            string& GetString(int n)
            {
                std::map
            <int,std::string>::iterator iter = strings.find(n);
                
            if (iter==strings.end())
                {
                    
            return strings[0];
                }
                
            return iter->second;
            }


            //main.cpp
            #include "stdafx.h"

            int main() 

                strings[
            0= "_0";
                strings[
            1= "_1";
                strings[
            2= "_2";

                std::
            string a = GetString(-1);

                std::
            string b = GetString(1);

                system(
            "pause");
                
            return 0;
            }

            //stdafx.h
            // stdafx.h : include file for standard system include files,
            // or project specific include files that are used frequently, but
            // are changed infrequently
            #pragma once
            #include 
            "targetver.h"
            #include 
            <stdio.h>
            #include 
            <stdlib.h>
            #include 
            <tchar.h>
            #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // some CString constructors will be explicit

            #include 
            <atlbase.h>
            #include 
            <atlstr.h>
            #include 
            <strsafe.h>
            #include 
            <string>
            #include 
            <algorithm>
            #include 
            <iostream>
            #include 
            <vector>
            #include 
            <time.h>
            #include 
            <stdio.h>
            #include 
            <stdlib.h>
            #include 
            <assert.h>
            #include 
            <map>
            #include 
            "1.h"

            posted on 2013-05-23 18:27 天下 閱讀(831) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            <2013年5月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            亚洲国产精品综合久久一线 | 久久乐国产综合亚洲精品| 久久精品国产精品亜洲毛片 | 色综合久久天天综合| 色青青草原桃花久久综合| jizzjizz国产精品久久| 欧美一级久久久久久久大| 色综合久久中文字幕无码| 国产精品久久久99| 久久不见久久见免费视频7| 精品免费久久久久国产一区| 久久久久无码精品国产不卡| 理论片午午伦夜理片久久 | 国产成人无码精品久久久久免费| 久久精品成人欧美大片| 久久精品视屏| 91精品婷婷国产综合久久| 亚洲午夜无码久久久久| 人妻无码久久精品| 久久青青草原亚洲av无码| 久久精品国产精品国产精品污| 久久99久国产麻精品66| 日本高清无卡码一区二区久久| 久久免费视频观看| 国产精品久久午夜夜伦鲁鲁| 中文精品久久久久人妻不卡| 久久只这里是精品66| 久久受www免费人成_看片中文| 日本久久久久久久久久| 精品欧美一区二区三区久久久| 国产精品99久久精品爆乳| 久久国产免费| 久久久久国色AV免费观看| 久久综合色区| yy6080久久| 色88久久久久高潮综合影院 | 人人狠狠综合久久亚洲88| 色综合久久中文综合网| 精品熟女少妇aⅴ免费久久| 久久久久久亚洲精品不卡 | 国产女人aaa级久久久级|