STL map的使用方法

2021-08-30 01:10:40 字數 4011 閱讀 9813

一map內部資料的組織

map內部自建一顆紅黑樹(一種非嚴格意義上的平衡二叉樹),這顆樹具有對資料自動排序的功能,所以在map內部所有的資料都是有序的,後邊我們會見識到有序的好處。 二

map的建構函式

map共提供了6個建構函式,這塊涉及到記憶體分配器這些東西,略過不表,在下面我們將接觸到一些map的構造方法,這裡要說下的就是,我們通常用如下方法構造乙個map:

mapmapstudent;

三資料的插入

在構造map容器後,我們就可以往裡面插入資料了。這裡講三種插入資料的方法:

第一種:用insert函式插入pair資料,下面舉例說明(以下**雖然是隨手寫的。在vc下**入這條語句,遮蔽4786警告#pragma warning (disable:4786) )

mapmapstudent;

mapstudent.insert(pair(1, 「student_one」));

mapstudent.insert(pair(2, 「student_two」));

mapstudent.insert(pair(3, 「student_three」));

map::iterator  iter;

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

第二種:用insert函式插入value_type資料,下面舉例說明

mapmapstudent;

mapstudent.insert(map::value_type (1, 「student_one」));

mapstudent.insert(map::value_type (2, 「student_two」));

mapstudent.insert(map::value_type (3, 「student_three」));

map::iterator  iter;

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

cout第三種:用陣列方式插入資料

mapmapstudent;

mapstudent[1] =  「student_one」;

mapstudent[2] =  「student_two」;

mapstudent[3] =  「student_three」;

區別:第一種和第二種在效果上是完成一樣的,用insert函式插入資料,在資料的插入上涉及到集合的唯一性這個概念,即當map中有這個關鍵字時,insert操作是插入資料不了的;但是用陣列方式就不同了,它可以覆蓋以前該關鍵字對應的值,用程式說明

mapstudent.insert(map::value_type (1, 「student_one」));

mapstudent.insert(map::value_type (1, 「student_two」));

驗證插入的成功性:上面這兩條語句執行後,map中1這個關鍵字對應的值是「student_one」,第二條語句並沒有生效,那麼這就涉及到我們怎麼知道insert語句是否插入成功的問題了,可以用pair來獲得是否插入成功,通過pair的第二個變數來知道是否插入成功,它的第乙個變數返回的是乙個map的迭代器,如果插入成功的話insert_pair.second應該是true的,否則為false。

程式如下

pair::iterator, bool> insert_pair;

insert_pair = mapstudent.insert(map::value_type (1, 「student_one」));

mapmapstudent;

pair::iterator, bool> insert_pair;

insert_pair =mapstudent.insert(pair(1, 「student_one」));

if(insert_pair.second == true)

else

陣列插入在資料覆蓋上的效果

mapstudent[1] =  「student_one」;

mapstudent[1] =  「student_two」;

三.        map的大小

int nsize = mapstudent.size();

四.       資料的遍歷

第一種:應用前向迭代器,上面舉例程式中到處都是了,略過不表

第二種:應用反相迭代器,下面舉例說明,要體會效果,請自個動手執行程式

map::reverse_iterator  iter;

for(iter = mapstudent.rbegin(); iter != mapstudent.rend(); iter++)

第三種:用陣列方式,程式說明如下

int nsize = mapstudent.size()

for(int nindex = 0; nindex < nsize; nindex++)

五.       資料的查詢(包括判定這個關鍵字是否在map中出現)

第一種:用count函式來判定關鍵字是否出現,其缺點是無法定位資料出現位置,由於map的特性,一對一的對映關係,就決定了count函式的返回值只有兩個,要麼是0,要麼是1,出現的情況,當然是返回1了

第二種:用find函式來定位資料出現位置,它返回的乙個迭代器,當資料出現時,它返回資料所在位置的迭代器,如果map中沒有要查詢的資料,它返回的迭代器等於end函式返回的迭代器,程式說明

mapstudent.insert(pair(1, 「student_one」));

mapstudent.insert(pair(2, 「student_two」));

mapstudent.insert(pair(3, 「student_three」));

map::iterator iter;

iter = mapstudent.find(1);

if(iter != mapstudent.end())

else

第三種:這個方法用來判定資料是否出現,是顯得笨了點,但是,我打算在這裡講解

lower_bound函式用法,這個函式用來返回要查詢關鍵字的下界(是乙個迭代器)

upper_bound函式用法,這個函式用來返回要查詢關鍵字的上界(是乙個迭代器)

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

equal_range函式返回乙個pair,pair裡面第乙個變數是lower_bound返回的迭代器,pair裡面第二個迭代器是upper_bound返回的迭代器,如果這兩個迭代器相等的話,則說明map中不出現這個關鍵字,程式說明

iter = mapstudent.lower_bound(3);

iter = mapstudent.upper_bound(2);

cout<<」do not find」< }

else

}studentinfo, *pstudentinfo;  //學生資訊

第二種:仿函式的應用,這個時候結構體中沒有直接的小於號過載,程式說明

typedef struct tagstudentinfo

studentinfo, *pstudentinfo;  //學生資訊

classs sort

}; int main()

十.   另外

由於stl是乙個統一的整體,map的很多用法都和stl中其它的東西結合在一起,比如在排序上,這裡預設用的是小於號,即less<>,如果要從大到小排序呢,這裡涉及到的東西很多,在此無法一一加以說明。

還要說明的是,map中由於它內部有序,由紅黑樹保證,因此很多函式執行的時間複雜度都是log2n的,如果用map函式可以實現的功能,而stl  algorithm也可以完成該功能,建議用map自帶函式,效率高一些。

下面說下,map在空間上的特性,否則,估計你用起來會有時候表現的比較鬱悶,由於map的每個資料對應紅黑樹上的乙個節點,這個節點在不儲存你的資料時,是占用16個位元組的,乙個父節點指標,左右孩子指標,還有乙個列舉值(標示紅黑的,相當於平衡二叉樹中的平衡因子)。

STL map的使用方法

方法一 使用insert進行插入 方法二 直接插入 逆序遍歷 map int,string reverse iterator it 逆序遍歷 for it m.rbegin iter m.rend it 順序遍歷 map int,string iterator it 方法一 用find函式 方法二 ...

正確使用stl map的erase方法

正確使用stl map的erase方法 stl的map表裡有乙個erase方法用來從乙個map中刪除掉指令的節點 eg map string string maptest typedef map string string iterator iter iter iter maptest.find k...

正確使用stl map的erase方法

先宣告 下面的文章是針對windows的用法,因為std map的erase函式的windows的實現版本是返回乙個std map的迭代器,但是stl標準裡面的該函式的返回值確是 map.erase有3個過載 void erase iterator position size type erase ...