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

            <2011年9月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            波多野结衣AV无码久久一区| 久久国产免费直播| 一本一本久久a久久精品综合麻豆| 色天使久久综合网天天| 久久er国产精品免费观看2| 99久久www免费人成精品| 无码国内精品久久综合88| 久久久久中文字幕| 久久99精品国产99久久| 99久久精品国内| 国产成人无码精品久久久免费| 久久精品国产亚洲AV大全| 久久综合噜噜激激的五月天| 久久人妻少妇嫩草AV蜜桃| 成人久久免费网站| 久久婷婷五月综合成人D啪| 久久精品亚洲精品国产色婷| 狠狠狠色丁香婷婷综合久久俺| 品成人欧美大片久久国产欧美| 久久久久久精品久久久久| 精品久久久久中文字| 嫩草伊人久久精品少妇AV| 一级做a爰片久久毛片看看| 久久精品a亚洲国产v高清不卡| 国产免费久久精品99久久| 色欲久久久天天天综合网| 欧美国产精品久久高清| 久久久久亚洲爆乳少妇无| 国产成人AV综合久久| 色播久久人人爽人人爽人人片aV| 无码伊人66久久大杳蕉网站谷歌 | 99久久无色码中文字幕人妻| 精品国际久久久久999波多野| 18岁日韩内射颜射午夜久久成人| 亚洲日本va午夜中文字幕久久| 久久午夜伦鲁片免费无码| 伊人久久无码中文字幕| 久久久综合香蕉尹人综合网| 久久99精品九九九久久婷婷| 69国产成人综合久久精品| 久久久久亚洲AV片无码下载蜜桃|