• <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 天下 閱讀(816) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            <2014年11月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            97精品依人久久久大香线蕉97| 久久精品国产99久久久| 久久精品亚洲日本波多野结衣 | 亚洲精品NV久久久久久久久久| 久久婷婷国产麻豆91天堂| 99久久精品免费看国产一区二区三区 | 久久久久国产一级毛片高清板| 99久久99这里只有免费费精品| 性欧美丰满熟妇XXXX性久久久| 久久人妻少妇嫩草AV蜜桃| 婷婷久久五月天| 久久精品久久久久观看99水蜜桃 | 高清免费久久午夜精品| 91精品国产高清久久久久久io| 国产91色综合久久免费分享| 91精品国产综合久久精品| 久久国产乱子伦精品免费强| 国产三级精品久久| 久久午夜综合久久| 国产亚洲精品久久久久秋霞 | 国产成人久久精品一区二区三区| 久久午夜福利无码1000合集| 99久久精品国产一区二区 | 亚洲国产成人精品女人久久久 | 麻豆AV一区二区三区久久| 久久久久亚洲av无码专区导航| 国产精品久久波多野结衣| 久久福利青草精品资源站| 精品久久久无码中文字幕| 日批日出水久久亚洲精品tv| 久久亚洲AV无码精品色午夜| 日产精品久久久久久久| 伊人久久大香线焦综合四虎| 亚洲国产成人久久综合碰| 色婷婷久久综合中文久久蜜桃av | 精品国产乱码久久久久软件| 精品久久久噜噜噜久久久| 欧美性大战久久久久久| 国产人久久人人人人爽| 一级做a爰片久久毛片免费陪| 日本福利片国产午夜久久|