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

            <2013年7月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            欧美伊人久久大香线蕉综合69| 久久久久免费视频| 色综合久久无码中文字幕| 亚洲中文字幕无码一久久区| 国产成人精品免费久久久久| 久久精品国产精品亜洲毛片| 欧美大香线蕉线伊人久久| 久久成人精品| 欧洲成人午夜精品无码区久久| 91久久精品无码一区二区毛片| 久久人人爽人人人人爽AV | 一本久久a久久精品vr综合| 久久精品欧美日韩精品| 国产精品无码久久四虎| 国产69精品久久久久观看软件 | 亚洲va久久久噜噜噜久久男同| 粉嫩小泬无遮挡久久久久久| 亚洲国产成人久久一区久久| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 精品久久久久久无码专区不卡| 久久播电影网| 9999国产精品欧美久久久久久| 欧美一级久久久久久久大| 久久97久久97精品免视看秋霞| 精品久久久久久久无码| 久久人人爽人人人人爽AV| 久久精品综合网| 综合久久给合久久狠狠狠97色| 66精品综合久久久久久久| 久久精品国产亚洲综合色| 色欲综合久久躁天天躁蜜桃| 久久亚洲精品无码aⅴ大香| 久久久久香蕉视频| 久久精品国产只有精品66| 91精品国产综合久久四虎久久无码一级| 亚洲AV日韩AV永久无码久久| 久久久久亚洲AV片无码下载蜜桃| 久久国产成人午夜AV影院| 伊人久久大香线蕉影院95| 国产免费久久精品丫丫| 久久精品国产一区|