• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

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


            以上三種用法,雖然都可以實現數據的插入,但是它們是有區別的,當然了第一種和第二種在效果上是完成一樣的,用insert函數插入數據,在數據的 插入上涉及到集合的唯一性這個概念,即當map中有這個關鍵字時,insert操作是插入數據不了的,但是用數組方式就不同了,它可以覆蓋以前該關鍵字對 應的值,用程序說明
            mapStudent.insert(map<int, string>::value_type (1, “student_one”));
            mapStudent.insert(map<int, string>::value_type (1, “student_two”));
            上面這兩條語句執行后,map中1這個關鍵字對應的值是“student_one”,第二條語句并沒有生效,那么這就涉及到我們怎么知道insert語句是否插入成功的問題了,可以用pair來獲得是否插入成功,程序如下
            Pair<map<int, string>::iterator, bool> Insert_Pair;
            Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
            我們通過pair的第二個變量來知道是否插入成功,它的第一個變量返回的是一個map的迭代器,如果插入成功的話Insert_Pair.second應該是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;
            }
            }
            大家可以用如下程序,看下用數組插入在數據覆蓋上的效果
            #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里面插入了數據,我們怎么知道當前已經插入了多少數據呢,可以用size函數,用法如下:
            Int nSize = mapStudent.size();
             
            4.       數據的遍歷
            這里也提供三種方法,對map進行遍歷
            第一種:應用前向迭代器,上面舉例程序中到處都是了,略過不表
            第二種:應用反相迭代器,下面舉例說明,要體會效果,請自個動手運行程序
            #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;
            }
            }
            第三種:用數組方式,程序說明如下
            #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()
            //此處有誤,應該是 for(int nIndex = 1; nIndex <= nSize; nIndex++)
            //by rainfish
            for(int nIndex = 0; nIndex < nSize; nIndex++)
            {
            Cout<<mapStudent[nIndex]<<end;
            }
            }

            aaa级精品久久久国产片| 久久久精品人妻无码专区不卡| 久久精品人人做人人爽电影| 亚洲欧美日韩中文久久| 亚洲国产成人久久精品影视| 欧美粉嫩小泬久久久久久久 | 狠狠色丁香婷综合久久| 人妻系列无码专区久久五月天| 色狠狠久久AV五月综合| 久久久久亚洲AV综合波多野结衣 | 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 国产精品久久久久久久久鸭| 久久e热在这里只有国产中文精品99 | 97久久国产综合精品女不卡| 激情综合色综合久久综合| 久久99精品国产麻豆| 国产精品久久久久蜜芽| 久久久免费观成人影院| 香蕉久久一区二区不卡无毒影院| 久久人人爽人人爽人人AV东京热| 欧美午夜精品久久久久久浪潮| 国产精品毛片久久久久久久| 中文字幕乱码久久午夜| 久久久这里只有精品加勒比| 久久精品亚洲福利| 久久996热精品xxxx| 99精品久久久久久久婷婷| 国内精品久久久久影院一蜜桃 | 久久99国产精一区二区三区| 色诱久久久久综合网ywww| 国产亚洲美女精品久久久2020| 中文字幕亚洲综合久久菠萝蜜| 久久久精品波多野结衣| 久久这里只有精品视频99| 久久人人超碰精品CAOPOREN| 国产亚洲成人久久| 久久强奷乱码老熟女| 免费精品国产日韩热久久| 精品国产乱码久久久久久人妻| 久久精品aⅴ无码中文字字幕不卡 久久精品成人欧美大片 | 久久久噜噜噜久久|