STL 之 map的用法

來源:互聯網
上載者:User

標籤:style   使用   os   strong   io   資料   for   ar   

       Map是STL的一個關聯容器,它提供一對一(其中第一個可以稱為關鍵字,每個關鍵字只能在map中出現一次,第二個可能稱為該關鍵字的值)的資料處理能力,由於這個特性,它完成有可能在我們處理一對一資料的時候,在編程上提供快速通道。這裡說下map內部資料的組織,map內部自建一顆紅/黑樹狀結構(一種非嚴格意義上的平衡二叉樹),這顆樹具有對資料自動排序的功能,所以在map內部所有的資料都是有序的,後邊我們會見識到有序的好處。

下面舉例說明什麼是一對一的資料對應。比如一個班級中,每個學生的學號跟他的姓名就存在著一一映射的關係,這個模型用map可能輕易描述,很明顯學號用int描述,姓名用字串描述(本篇文章中不用char *來描述字串,而是採用STL中string來描述),下面給出map描述代碼:Map<int, string> mapStudent;
1.       map的建構函式
map共提供了6個建構函式,這塊涉及到記憶體 Clerk這些東西,略過不表,在下面我們將接觸到一些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;
}
}

以下為對pair的解釋:

——————————————————————————————————————————————

Pair類型概述

pair是一種模板類型,其中包含兩個資料值,兩個資料的類型可以不同,基本的定義如下:

 

pair<int, string> a;

表示a中有兩個類型,第一個元素是int型的,第二個元素是string類型的,如果建立pair的時候沒有對其進行初始化,則調用預設建構函式對其初始化。

 

pair<string, string> a("James", "Joy");

也可以像上面一樣在定義的時候直接對其初始化。

 

由於pair類型的使用比較繁瑣,因為如果要定義多個形同的pair類型的時候,可以時候typedef簡化聲明:

typedef pair<string, string> author;

author pro("May", "Lily");

author joye("James", "Joyce");

 

 

Pair對象的操作

 

  • 對於pair類,由於它只有兩個元素,分別名為first和second,因此直接使用普通的點操作符即可訪問其成員

pair<string, string> a("Lily", "Poly"); 

string name;

name = pair.second;

  • 產生新的pair對象

可以使用make_pair對已存在的兩個資料構造一個新的pair類型:

int a = 8;

string m = "James";

pair<int, string> newone;

newone = make_pair(a, m);

 

 ——————————————————————————————————————————————————————————————

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;
}
}
5.       資料的尋找(包括判定這個關鍵字是否在map中出現)
在這裡我們將體會,map在資料插入時保證有序的好處。
要判定一個資料(關鍵字)是否在map中出現的方法比較多,這裡標題雖然是資料的尋找,在這裡將穿插著大量的

map基本用法。
這裡給出三種資料尋找方法
第一種:用count函數來判定關鍵字是否出現,其缺點是無法定位元據出現位置,由於map的特性,一對一的映射關係

,就決定了count函數的傳回值只有兩個,要麼是0,要麼是1,出現的情況,當然是返回1了
第二種:用find函數來定位元據出現位置,它返回的一個迭代器,當資料出現時,它返回資料所在位置的迭代器,

如果map中沒有要尋找的資料,它返回的迭代器等於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”));
       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;
}
}
第三種:這個方法用來判定資料是否出現,是顯得笨了點,但是,我打算在這裡講解
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中不出現這個關鍵字,程式說明
#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.       資料的清空與判空
清空map中的資料可以用clear()函數,判定map中是否有資料可以用empty()函數,它返回true則說明是空map
7.       資料的刪除
這裡要用到erase函數,它有三個重載了的函數,下面在例子中詳細說明它們的用法

#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的特性,刪除區間是一個前閉後開的集合
       //自個加上遍曆代碼,列印輸出吧
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.