最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談c++中的stl中的map用法詳解

 更新時間:2016年10月30日 12:11:10   投稿:jingxian  
下面小編就為大家?guī)硪黄獪\談c++中的stl中的map用法詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Map是STL的一個關聯(lián)容器,它提供一對一(其中第一個可以稱為關鍵字,每個關鍵字只能在map中出現(xiàn)一次,第二個可能稱為該關鍵字的值)的數(shù)據(jù)處理能力,由于這個特性,它完成有可能在我們處理一對一數(shù)據(jù)的時候,在編程上提供快速通道。這里說下map內(nèi)部數(shù)據(jù)的組織,map內(nèi)部自建一顆紅黑樹(一種非嚴格意義上的平衡二叉樹),這顆樹具有對數(shù)據(jù)自動排序的功能,所以在map內(nèi)部所有的數(shù)據(jù)都是有序的,后邊我們會見識到有序的好處。

下面舉例說明什么是一對一的數(shù)據(jù)映射。比如一個班級中,每個學生的學號跟他的姓名就存在著一一映射的關系,這個模型用map可能輕易描述,很明顯學號用int描述,姓名用字符串描述(本篇文章中不用char *來描述字符串,而是采用STL中string來描述),下面給出map描述代碼:

Map<int, string> mapStudent;

1. map的構造函數(shù)

map共提供了6個構造函數(shù),這塊涉及到內(nèi)存分配器這些東西,略過不表,在下面我們將接觸到一些map的構造方法,這里要說下的就是,我們通常用如下方法構造一個map:

Map<int, string> mapStudent;

2. 數(shù)據(jù)的插入

在構造map容器后,我們就可以往里面插入數(shù)據(jù)了。這里講三種插入數(shù)據(jù)的方法:

第一種:用insert函數(shù)插入pair數(shù)據(jù),下面舉例說明(以下代碼雖然是隨手寫的,應該可以在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函數(shù)插入value_type數(shù)據(jù),下面舉例說明

#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ù),下面舉例說明

#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;

}

}

以上三種用法,雖然都可以實現(xiàn)數(shù)據(jù)的插入,但是它們是有區(qū)別的,當然了第一種和第二種在效果上是完成一樣的,用insert函數(shù)插入數(shù)據(jù),在數(shù)據(jù)的插入上涉及到集合的唯一性這個概念,即當map中有這個關鍵字時,insert操作是插入數(shù)據(jù)不了的,但是用數(shù)組方式就不同了,它可以覆蓋以前該關鍵字對應的值,用程序說明

mapStudent.insert(map<int, string>::value_type (1, “student_one”));

mapStudent.insert(map<int, string>::value_type (1, “student_two”));

上面這兩條語句執(zhí)行后,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;

}

}

大家可以用如下程序,看下用數(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ù),我們怎么知道當前已經(jīng)插入了多少數(shù)據(jù)呢,可以用size函數(shù),用法如下:

Int nSize = mapStudent.size();

4. 數(shù)據(jù)的遍歷

這里也提供三種方法,對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ù)組方式,程序說明如下

#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;

}

}

5. 數(shù)據(jù)的查找(包括判定這個關鍵字是否在map中出現(xiàn))

在這里我們將體會,map在數(shù)據(jù)插入時保證有序的好處。

要判定一個數(shù)據(jù)(關鍵字)是否在map中出現(xiàn)的方法比較多,這里標題雖然是數(shù)據(jù)的查找,在這里將穿插著大量的map基本用法。

這里給出三種數(shù)據(jù)查找方法

第一種:用count函數(shù)來判定關鍵字是否出現(xiàn),其缺點是無法定位數(shù)據(jù)出現(xiàn)位置,由于map的特性,一對一的映射關系,就決定了count函數(shù)的返回值只有兩個,要么是0,要么是1,出現(xiàn)的情況,當然是返回1了

第二種:用find函數(shù)來定位數(shù)據(jù)出現(xiàn)位置,它返回的一個迭代器,當數(shù)據(jù)出現(xiàn)時,它返回數(shù)據(jù)所在位置的迭代器,如果map中沒有要查找的數(shù)據(jù),它返回的迭代器等于end函數(shù)返回的迭代器,程序說明

#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;

    iter = mapStudent.find(1);

if(iter != mapStudent.end())

{

    Cout<<”Find, the value is ”<<iter->second<<endl;

}

Else

{

    Cout<<”Do not Find”<<endl;

}

}

第三種:這個方法用來判定數(shù)據(jù)是否出現(xiàn),是顯得笨了點,但是,我打算在這里講解

Lower_bound函數(shù)用法,這個函數(shù)用來返回要查找關鍵字的下界(是一個迭代器)

Upper_bound函數(shù)用法,這個函數(shù)用來返回要查找關鍵字的上界(是一個迭代器)

例如:map中已經(jīng)插入了1,2,3,4的話,如果lower_bound(2)的話,返回的2,而upper-bound(2)的話,返回的就是3

Equal_range函數(shù)返回一個pair,pair里面第一個變量是Lower_bound返回的迭代器,pair里面第二個迭代器是Upper_bound返回的迭代器,如果這兩個迭代器相等的話,則說明map中不出現(xiàn)這個關鍵字,程序說明

#include <map>

#include <string>

#include <iostream>

Using namespace std;

Int main()

{

    Map<int, string> mapStudent;

    mapStudent[1] = “student_one”;

    mapStudent[3] = “student_three”;

    mapStudent[5] = “student_five”;

    map<int, string>::iterator iter;

iter = mapStudent.lower_bound(2);

{

    //返回的是下界3的迭代器

    Cout<<iter->second<<endl;

}

iter = mapStudent.lower_bound(3);

{

    //返回的是下界3的迭代器

    Cout<<iter->second<<endl;

}

 

iter = mapStudent.upper_bound(2);

{

    //返回的是上界3的迭代器

    Cout<<iter->second<<endl;

}

iter = mapStudent.upper_bound(3);

{

    //返回的是上界5的迭代器

    Cout<<iter->second<<endl;

}

 

Pair<map<int, string>::iterator, map<int, string>::iterator> mapPair;

mapPair = mapStudent.equal_range(2);

if(mapPair.first == mapPair.second)
    {

    cout<<”Do not Find”<<endl;

}

Else

{

Cout<<”Find”<<endl;
}

mapPair = mapStudent.equal_range(3);

if(mapPair.first == mapPair.second)
    {

    cout<<”Do not Find”<<endl;

}

Else

{

Cout<<”Find”<<endl;
}

}

6. 數(shù)據(jù)的清空與判空

清空map中的數(shù)據(jù)可以用clear()函數(shù),判定map中是否有數(shù)據(jù)可以用empty()函數(shù),它返回true則說明是空map

7. 數(shù)據(jù)的刪除

這里要用到erase函數(shù),它有三個重載了的函數(shù),下面在例子中詳細說明它們的用法

#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”));

 

//如果你要演示輸出效果,請選擇以下的一種,你看到的效果會比較好

    //如果要刪除1,用迭代器刪除

    map<int, string>::iterator iter;

    iter = mapStudent.find(1);

    mapStudent.erase(iter);

 

    //如果要刪除1,用關鍵字刪除

    Int n = mapStudent.erase(1);//如果刪除了會返回1,否則返回0

 

    //用迭代器,成片的刪除

    //一下代碼把整個map清空

    mapStudent.earse(mapStudent.begin(), mapStudent.end());

    //成片刪除要注意的是,也是STL的特性,刪除區(qū)間是一個前閉后開的集合

 

    //自個加上遍歷代碼,打印輸出吧

}

8. 其他一些函數(shù)用法

這里有swap,key_comp,value_comp,get_allocator等函數(shù),感覺到這些函數(shù)在編程用的不是很多,略過不表,有興趣的話可以自個研究

9. 排序

這里要講的是一點比較高深的用法了,排序問題,STL中默認是采用小于號來排序的,以上代碼在排序上是不存在任何問題的,因為上面的關鍵字是int型,它本身支持小于號運算,在一些特殊情況,比如關鍵字是一個結構體,涉及到排序就會出現(xiàn)問題,因為它沒有小于號操作,insert等函數(shù)在編譯的時候過不去,下面給出兩個方法解決這個問題

第一種:小于號重載,程序舉例

#include <map>

#include <string>

Using namespace std;

Typedef struct tagStudentInfo

{

    Int   nID;

    String  strName;

}StudentInfo, *PStudentInfo; //學生信息

 

Int main()

{

  int nSize;

    //用學生信息映射分數(shù)

    map<StudentInfo, int>mapStudent;

  map<StudentInfo, int>::iterator iter;

    StudentInfo studentInfo;

    studentInfo.nID = 1;

    studentInfo.strName = “student_one”;

    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));

    studentInfo.nID = 2;

    studentInfo.strName = “student_two”;

mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));

 

for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)

  cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;

 

}

以上程序是無法編譯通過的,只要重載小于號,就OK了,如下:

Typedef struct tagStudentInfo

{

    Int   nID;

    String  strName;

    Bool operator < (tagStudentInfo const& _A) const

    {

       //這個函數(shù)指定排序策略,按nID排序,如果nID相等的話,按strName排序

       If(nID < _A.nID) return true;

       If(nID == _A.nID) return strName.compare(_A.strName) < 0;

       Return false;

    }

}StudentInfo, *PStudentInfo; //學生信息

第二種:仿函數(shù)的應用,這個時候結構體中沒有直接的小于號重載,程序說明

#include <map>

#include <string>

Using namespace std;

Typedef struct tagStudentInfo

{

    Int   nID;

    String  strName;

}StudentInfo, *PStudentInfo; //學生信息

 

Classs sort

{

    Public:

    Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const

    {

       If(_A.nID < _B.nID) return true;

       If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;

       Return false;

    }

};

 

Int main()

{

    //用學生信息映射分數(shù)

    Map<StudentInfo, int, sort>mapStudent;

    StudentInfo studentInfo;

    studentInfo.nID = 1;

    studentInfo.strName = “student_one”;

    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));

    studentInfo.nID = 2;

    studentInfo.strName = “student_two”;

mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));

}

10. 另外

由于STL是一個統(tǒng)一的整體,map的很多用法都和STL中其它的東西結合在一起,比如在排序上,這里默認用的是小于號,即less<>,如果要從大到小排序呢,這里涉及到的東西很多,在此無法一一加以說明。

還要說明的是,map中由于它內(nèi)部有序,由紅黑樹保證,因此很多函數(shù)執(zhí)行的時間復雜度都是log2N的,如果用map函數(shù)可以實現(xiàn)的功能,而STL  Algorithm也可以完成該功能,建議用map自帶函數(shù),效率高一些。

下面說下,map在空間上的特性,否則,估計你用起來會有時候表現(xiàn)的比較郁悶,由于map的每個數(shù)據(jù)對應紅黑樹上的一個節(jié)點,這個節(jié)點在不保存你的數(shù)據(jù)時,是占用16個字節(jié)的,一個父節(jié)點指針,左右孩子指針,還有一個枚舉值(標示紅黑的,相當于平衡二叉樹中的平衡因子),我想大家應該知道,這些地方很費內(nèi)存了吧,不說了……

以上就是小編為大家?guī)淼臏\談c++中的stl中的map用法詳解全部內(nèi)容了,希望大家多多支持腳本之家~

相關文章

  • C++并查集常用操作

    C++并查集常用操作

    并查集 是一種樹型的數(shù)據(jù)結構,用于處理一些不相加集合的合并和查詢問題。本文給大家分享C++并查集常用操作及算法實現(xiàn),感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • C++解析ini文件的實現(xiàn)方法

    C++解析ini文件的實現(xiàn)方法

    在C++編程中,有時我們需要處理配置文件來存儲應用程序的設置和參數(shù),而INI文件是一種常見的選擇,這篇文章主要給大家介紹了關于C++解析ini文件的實現(xiàn)方法,需要的朋友可以參考下
    2024-08-08
  • 超級詳細講解C++中的多態(tài)

    超級詳細講解C++中的多態(tài)

    多態(tài)是在不同繼承關系的類對象,去調(diào)同一函數(shù),產(chǎn)生了不同的行為,下面這篇文章主要給大家介紹了關于C++中多態(tài)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • C++分析構造函數(shù)與析造函數(shù)的特點梳理

    C++分析構造函數(shù)與析造函數(shù)的特點梳理

    本文對類的構造函數(shù)和析構函數(shù)進行總結,主要包括了構造函數(shù)的初始化、重載、使用參數(shù)和默認參數(shù),拷貝構造函數(shù)和析構函數(shù),希望能幫助讀者在程序開發(fā)中更好的理解類,屬于C/C++基礎
    2022-05-05
  • 一文帶你學會C語言中的qsort函數(shù)

    一文帶你學會C語言中的qsort函數(shù)

    qsort函數(shù)是C語言的庫函數(shù),能實現(xiàn)對各種元素類型的比較,使用的基本思想是快速排序法,頭文件是<stdlib.h>,本文不講解具體實現(xiàn)原理,只對使用方法進行說明,希望對大家有所幫助
    2022-12-12
  • C語言實現(xiàn)2048游戲(ege圖形庫版)

    C語言實現(xiàn)2048游戲(ege圖形庫版)

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)2048游戲,ege圖形庫版,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • C++初識類和對象

    C++初識類和對象

    類是創(chuàng)建對象的模板,一個類可以創(chuàng)建多個對象,每個對象都是類類型的一個變量;創(chuàng)建對象的過程也叫類的實例化。每個對象都是類的一個具體實例(Instance),擁有類的成員變量和成員函數(shù)
    2021-10-10
  • C語言的strcpy函數(shù)你了解嗎

    C語言的strcpy函數(shù)你了解嗎

    這篇文章主要為大家詳細介紹了C語言的strcpy函數(shù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • C語言實現(xiàn)520表白代碼 祝你表白成功!

    C語言實現(xiàn)520表白代碼 祝你表白成功!

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)520表白代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • VS2019+MPI配置過程的實現(xiàn)步驟

    VS2019+MPI配置過程的實現(xiàn)步驟

    本文介紹了在VS2019上配置MPI,包括下載和安裝MPI、創(chuàng)建項目、配置屬性、導入頭文件和庫文件、添加依賴項等步驟,具有一定的參考價值,感興趣的可以了解一下
    2024-12-12

最新評論

濉溪县| 平顶山市| 牙克石市| 潍坊市| 厦门市| 康定县| 东港市| 东山县| 武汉市| 盐亭县| 美姑县| 万山特区| 滨海县| 义乌市| 松滋市| 威信县| 苏尼特左旗| 大港区| 浑源县| 霍城县| 宜兰市| 博客| 平陆县| 腾冲县| 台山市| 新安县| 和龙市| 云霄县| 新疆| 平利县| 读书| 潮州市| 襄垣县| 剑河县| 肥城市| 和龙市| 玛曲县| 茂名市| 抚顺县| 营山县| 囊谦县|