• <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>
            posts - 311, comments - 0, trackbacks - 0, articles - 0
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            Map是STL的一個(gè)關(guān)聯(lián)容器,它提供一對(duì)一(其中第一個(gè)可以稱為關(guān)鍵字,每個(gè)關(guān)鍵字只能在map中出現(xiàn)一次,第二個(gè)可能稱為該關(guān)鍵字的值)的數(shù)據(jù) 處理能力,由于這個(gè)特性,它完成有可能在我們處理一對(duì)一數(shù)據(jù)的時(shí)候,在編程上提供快速通道。這里說(shuō)下map內(nèi)部數(shù)據(jù)的組織,map內(nèi)部自建一顆紅黑樹(一 種非嚴(yán)格意義上的平衡二叉樹),這顆樹具有對(duì)數(shù)據(jù)自動(dòng)排序的功能,所以在map內(nèi)部所有的數(shù)據(jù)都是有序的,后邊我們會(huì)見識(shí)到有序的好處。
            下面舉例說(shuō)明什么是一對(duì)一的數(shù)據(jù)映射。比如一個(gè)班級(jí)中,每個(gè)學(xué)生的學(xué)號(hào)跟他的姓名就存在著一一映射的關(guān)系,這個(gè)模型用map可能輕易描述,很明顯學(xué) 號(hào)用int描述,姓名用字符串描述(本篇文章中不用char *來(lái)描述字符串,而是采用STL中string來(lái)描述),下面給出map描述代碼:
            Map<int, string> mapStudent;
             
            1.       map的構(gòu)造函數(shù)
            map共提供了6個(gè)構(gòu)造函數(shù),這塊涉及到內(nèi)存分配器這些東西,略過不表,在下面我們將接觸到一些map的構(gòu)造方法,這里要說(shuō)下的就是,我們通常用如下方法構(gòu)造一個(gè)map:
            Map<int, string> mapStudent;
             
            2.       數(shù)據(jù)的插入
            在構(gòu)造map容器后,我們就可以往里面插入數(shù)據(jù)了。這里講三種插入數(shù)據(jù)的方法:
            第一種:用insert函數(shù)插入pair數(shù)據(jù),下面舉例說(shuō)明(以下代碼雖然是隨手寫的,應(yīng)該可以在VC和GCC下編譯通過,大家可以運(yùn)行下看什么效果,在VC下請(qǐng)加入這條語(yǔ)句,屏蔽4786警告 #pragma warning (disable:4786) )


            #include <map>
            #include <string>
            #include <iostream>
            Using namespace std;
            Int main()
            {
            Map<int, string> mapStudent;
            mapStudent.insert(pair<int, string>(1, “student_one”));
            mapStudent.insert(pair<int, string>(2, “student_two”));
            mapStudent.insert(pair<int, string>(3, “student_three”));
            map<int, string>::iterator iter;
            for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
            {
            Cout<<iter->first<<”   ”<<iter->second<<end;
            }
            }
            第二種:用insert函數(shù)插入value_type數(shù)據(jù),下面舉例說(shuō)明
            #include <map>
            #include <string>
            #include <iostream>
            Using namespace std;
            Int main()
            {
            Map<int, string> mapStudent;
            mapStudent.insert(map<int, string>::value_type (1, “student_one”));
            mapStudent.insert(map<int, string>::value_type (2, “student_two”));
            mapStudent.insert(map<int, string>::value_type (3, “student_three”));
            map<int, string>::iterator iter;
            for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
            {
            Cout<<iter->first<<”   ”<<iter->second<<end;
            }
            }
            第三種:用數(shù)組方式插入數(shù)據(jù),下面舉例說(shuō)明
            #include <map>
            #include <string>
            #include <iostream>
            Using namespace std;
            Int main()
            {
            Map<int, string> mapStudent;
            mapStudent[1] = “student_one”;
            mapStudent[2] = “student_two”;
            mapStudent[3] = “student_three”;
            map<int, string>::iterator iter;
            for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
            {
            Cout<<iter->first<<”   ”<<iter->second<<end;
            }
            }


            以上三種用法,雖然都可以實(shí)現(xiàn)數(shù)據(jù)的插入,但是它們是有區(qū)別的,當(dāng)然了第一種和第二種在效果上是完成一樣的,用insert函數(shù)插入數(shù)據(jù),在數(shù)據(jù)的 插入上涉及到集合的唯一性這個(gè)概念,即當(dāng)map中有這個(gè)關(guān)鍵字時(shí),insert操作是插入數(shù)據(jù)不了的,但是用數(shù)組方式就不同了,它可以覆蓋以前該關(guān)鍵字對(duì) 應(yīng)的值,用程序說(shuō)明
            mapStudent.insert(map<int, string>::value_type (1, “student_one”));
            mapStudent.insert(map<int, string>::value_type (1, “student_two”));
            上面這兩條語(yǔ)句執(zhí)行后,map中1這個(gè)關(guān)鍵字對(duì)應(yīng)的值是“student_one”,第二條語(yǔ)句并沒有生效,那么這就涉及到我們?cè)趺粗纈nsert語(yǔ)句是否插入成功的問題了,可以用pair來(lái)獲得是否插入成功,程序如下
            Pair<map<int, string>::iterator, bool> Insert_Pair;
            Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
            我們通過pair的第二個(gè)變量來(lái)知道是否插入成功,它的第一個(gè)變量返回的是一個(gè)map的迭代器,如果插入成功的話Insert_Pair.second應(yīng)該是true的,否則為false。
            下面給出完成代碼,演示插入成功與否問題

            #include <map>
            #include <string>
            #include <iostream>
            Using namespace std;
            Int main()
            {
            Map<int, string> mapStudent;
            Pair<map<int, string>::iterator, bool> Insert_Pair;
            Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
            If(Insert_Pair.second == true)
            {
            Cout<<”Insert Successfully”<<endl;
            }
            Else
            {
            Cout<<”Insert Failure”<<endl;
            }
            Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_two”));
            If(Insert_Pair.second == true)
            {
            Cout<<”Insert Successfully”<<endl;
            }
            Else
            {
            Cout<<”Insert Failure”<<endl;
            }
            map<int, string>::iterator iter;
            for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
            {
            Cout<<iter->first<<”   ”<<iter->second<<end;
            }
            }
            大家可以用如下程序,看下用數(shù)組插入在數(shù)據(jù)覆蓋上的效果
            #include <map>
            #include <string>
            #include <iostream>
            Using namespace std;
            Int main()
            {
            Map<int, string> mapStudent;
            mapStudent[1] = “student_one”;
            mapStudent[1] = “student_two”;
            mapStudent[2] = “student_three”;
            map<int, string>::iterator iter;
            for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
            {
            Cout<<iter->first<<”   ”<<iter->second<<end;
            }
            }
             
            3.       map的大小
            在往map里面插入了數(shù)據(jù),我們?cè)趺粗喇?dāng)前已經(jīng)插入了多少數(shù)據(jù)呢,可以用size函數(shù),用法如下:
            Int nSize = mapStudent.size();
             
            4.       數(shù)據(jù)的遍歷
            這里也提供三種方法,對(duì)map進(jìn)行遍歷
            第一種:應(yīng)用前向迭代器,上面舉例程序中到處都是了,略過不表
            第二種:應(yīng)用反相迭代器,下面舉例說(shuō)明,要體會(huì)效果,請(qǐng)自個(gè)動(dòng)手運(yùn)行程序
            #include <map>
            #include <string>
            #include <iostream>
            Using namespace std;
            Int main()
            {
            Map<int, string> mapStudent;
            mapStudent.insert(pair<int, string>(1, “student_one”));
            mapStudent.insert(pair<int, string>(2, “student_two”));
            mapStudent.insert(pair<int, string>(3, “student_three”));
            map<int, string>::reverse_iterator iter;
            for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
            {
            Cout<<iter->first<<”   ”<<iter->second<<end;
            }
            }
            第三種:用數(shù)組方式,程序說(shuō)明如下
            #include <map>
            #include <string>
            #include <iostream>
            Using namespace std;
            Int main()
            {
            Map<int, string> mapStudent;
            mapStudent.insert(pair<int, string>(1, “student_one”));
            mapStudent.insert(pair<int, string>(2, “student_two”));
            mapStudent.insert(pair<int, string>(3, “student_three”));
            int nSize = mapStudent.size()
            //此處有誤,應(yīng)該是 for(int nIndex = 1; nIndex <= nSize; nIndex++)
            //by rainfish
            for(int nIndex = 0; nIndex < nSize; nIndex++)
            {
            Cout<<mapStudent[nIndex]<<end;
            }
            }

            国内精品九九久久精品| 久久无码人妻精品一区二区三区 | 亚洲日韩中文无码久久| 漂亮人妻被中出中文字幕久久 | 国产午夜精品理论片久久影视 | 国产午夜久久影院| 亚洲国产精品综合久久网络 | 久久久久av无码免费网| 99国产欧美精品久久久蜜芽| 国产日韩久久免费影院| 久久久久久亚洲AV无码专区| 精品熟女少妇aⅴ免费久久| 久久人人爽人人爽人人片AV高清| 国产国产成人精品久久| 亚洲七七久久精品中文国产| 久久99国产精品久久| 亚洲国产美女精品久久久久∴ | 久久综合亚洲色一区二区三区| 久久精品草草草| 亚洲午夜久久久影院伊人| 99久久精品国产毛片| 国内精品久久久久伊人av| 少妇无套内谢久久久久| 久久精品国产精品亚洲人人 | 日批日出水久久亚洲精品tv| 久久久久四虎国产精品| 久久久久亚洲AV无码麻豆| 漂亮人妻被中出中文字幕久久| 久久免费99精品国产自在现线| 久久99国产精品久久久| 99久久久精品| 97久久超碰成人精品网站| 久久精品国产久精国产一老狼| 日韩十八禁一区二区久久| 狠狠人妻久久久久久综合| 久久婷婷久久一区二区三区| 久久国产精品-久久精品| 99久久国产综合精品五月天喷水 | 午夜精品久久久久久久无码| 久久精品国产只有精品66| 日日狠狠久久偷偷色综合0|