• <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)

            鏈接

            最新隨筆

            搜索

            最新評論

            午夜视频久久久久一区 | 久久99精品国产自在现线小黄鸭| 久久久久久无码国产精品中文字幕| 亚洲国产精品无码久久久秋霞2| 久久中文字幕视频、最近更新| 亚洲人成无码www久久久| 国产精品成人久久久| 综合久久国产九一剧情麻豆| 欧美噜噜久久久XXX| 久久中文字幕人妻熟av女| 精品水蜜桃久久久久久久| …久久精品99久久香蕉国产| 奇米综合四色77777久久| 狠狠综合久久综合88亚洲| 国产女人aaa级久久久级| 精品久久久久久国产三级| 国产精品久久久久天天影视| 久久91精品国产91久久小草| 99久久免费国产精品热| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久九九有精品国产23百花影院| 久久久国产乱子伦精品作者| 久久精品国产亚洲av影院| AV无码久久久久不卡蜜桃| 精品久久久久久国产三级| 无码人妻久久一区二区三区蜜桃 | 性高湖久久久久久久久| 老色鬼久久亚洲AV综合| 久久久久亚洲国产| 久久成人小视频| 中文字幕亚洲综合久久菠萝蜜 | 久久午夜综合久久| 国产精品免费久久久久久久久 | 久久人做人爽一区二区三区 | 伊人久久综合成人网| 国产精品久久婷婷六月丁香| 久久天天日天天操综合伊人av| 理论片午午伦夜理片久久| 亚洲AV无码久久精品成人 | 欧美久久天天综合香蕉伊| 大伊人青草狠狠久久|