STL--map用法
STL--map用法
map是STL的一個關(guān)聯(lián)容器,它提供一對一(其中第一個可以稱為關(guān)鍵字,每個關(guān)鍵字只能在map中出現(xiàn)一次,第二個可能稱為該關(guān)鍵字的值)的數(shù)據(jù)處理能力由于這個特性它完成有可能在我們處理一對一數(shù)據(jù)的時候,在編程上提供快速通道。這里說下map內(nèi)部數(shù)據(jù)的組織map內(nèi)部自建一顆紅黑樹(一種非嚴(yán)格意義上的平衡二叉樹),這顆樹具有對數(shù)據(jù)自動排序的功能,所以在map內(nèi)部所有的數(shù)據(jù)都是有序的,后邊我們會見識到有序的好處。
下面舉例說明什么是一對一的數(shù)據(jù)映射。比如一個班級中,每個學(xué)生的學(xué)號跟他的姓名就存在著一一映射的關(guān)系,這個模型用map可能輕易描述,很明顯學(xué)號用int描述,姓名用字符串描述(本篇文章中不用char *來描述字符串,而是采用STL中string來描述),下面給出map描述代碼:
map<int, string> mymap;
1.數(shù)據(jù)的插入
(1)用insert函數(shù)插入value_type數(shù)據(jù),下面舉例說明
#include <map>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
?????? map<int,string> mymap;
?????? mymap.insert(map<int,string>::value_type(1,"student_one"));
?????? mymap.insert(map<int,string>::value_type(2,"student_two"));
?????? mymap.insert(map<int,string>::value_type(3,"student_three"));
?????? map<int,string>::iterator it;
?????? for(it= mymap.begin();it!=mymap.end();it++)
??????? {
??????????????? cout << it->first << " " << it->second << endl;
??????? }
}
?
(2)用數(shù)組方式插入數(shù)據(jù),下面舉例說明
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
?????? map<int,string> mymap;
?????? mymap[1]="student_one";
?????? mymap[2]="student_two";
?????? mymap[3]="student_three";
?????? map<int,string>::iterator it;
?????? for(it=mymap.begin();it!= mymap.end();it++)
??????? {
??????????? cout<<it->first<<" "<<it->second<< endl;
??????? }
}
以上兩種用法,雖然都可以實現(xiàn)數(shù)據(jù)的插入,但是它們是有區(qū)別的,用insert函數(shù)插入數(shù)據(jù),在數(shù)據(jù)的插入上涉及到集合的唯一性這個概念,即當(dāng)map中有這個關(guān)鍵字時,insert操作是插入數(shù)據(jù)不了的,但是用數(shù)組方式就不同了,它可以覆蓋以前該關(guān)鍵字對應(yīng)的值,用程序說明
mymap.insert(map<int, string>::value_type (1, "student_one"));
mymap.insert(map<int, string>::value_type (1, "student_two"));
上面這兩條語句執(zhí)行后,map中1這個關(guān)鍵字對應(yīng)的值是"student_one",第二條語句并沒有生效,那么這就涉及到我們怎么知道insert語句是否插入成功的問題了,可以用pair來獲得是否插入成功,程序如下
?
pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mymap.insert(map<int, string>::value_type (1, "student_one"));
?
我們通過pair的第二個變量來知道是否插入成功,它的第一個變量返回的是一個map的迭代器,如果插入成功的話Insert_Pair.second應(yīng)該是true的,否則為false。
?
下面給出完成代碼,演示插入成功與否問題
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
?????? map<int,string> mymap;
?????? pair<map<int,string>::iterator,bool> Insert_pair;
?????? Insert_pair=mymap.insert(pair<int, string>(1,"student_one"));
?????? if(Insert_pair.second==true)
?????? {
?????????????? cout << "Insert Successfully" << endl;
?????? }
?????? else??? cout << "Insert Failure" << endl;
?????? Insert_pair=mymap.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 it;
?????? for(it = mymap.begin(); it != mymap.end(); it++)
??????? {
??????????? cout << it->first << " " << it->second << endl;
??????? }
}
運行結(jié)果:
Insert Successfully
Insert Failure
1? student_one
那么我們可以用如下程序,看下用數(shù)組插入在數(shù)據(jù)覆蓋上的效果
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
?????? map<int,string> mymap;
?????? mymap[1]= "student_one";
?????? mymap[1]= "student_two";
?????? mymap[2]= "student_three";
?????? map<int,string>::iterator it;
?????? for(it=mymap.begin();it!=mymap.end();it++)
??????? {
??????????? cout << it->first << " " << it->second << endl;
??????? }
??????? return 0;
}
運行結(jié)果:
1? student_two
2? student_three
?
2.?? map的大小
在往map里面插入了數(shù)據(jù),我們怎么知道當(dāng)前已經(jīng)插入了多少數(shù)據(jù)呢,可以用size函數(shù),用法如下:
Int nsize = mymap;.size();
3.? 數(shù)據(jù)的遍歷
這里提供三種方法,對map進行遍歷
第一種:應(yīng)用前向迭代器,上面舉例程序中到處都是了,略過不表
第二種:應(yīng)用反相迭代器,下面舉例說明,要體會效果,以下為運行程序
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
?????? map<int,string> mymap;
?????? mymap.insert(map<int,string>::value_type(1,"student_one"));
?????? mymap.insert(map<int,string>::value_type(2,"student_two"));
?????? mymap.insert(map<int,string>::value_type(3,"student_three"));
?????? map<int,string>::reverse_iterator it;
?????? for(it=mymap.rbegin();it!=mymap.rend();it++)
??????? {
??????????? cout << it->first << " "<< it->second << endl;
??????? }
}
運行結(jié)果:
3? student_three
2? student_two
1? student_one
第三種:用數(shù)組方式遍歷,程序說明如下
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
?????? map<int,string> mymap;
?????? mymap.insert(map<int,string>::value_type(1,"student_one"));
?????? mymap.insert(map<int,string>::value_type(2,"student_two"));
?????? mymap.insert(map<int,string>::value_type(3,"student_three"));
?????? int nsize = mymap.size();
?????? for(int i=1;i<= nsize;i++)
??????? {
??????????? cout << mymap[i] << endl;
??????? }
}
運行結(jié)果:
1? student_one
2? student_two
3? student_three
4. 數(shù)據(jù)的查找(包括判定這個關(guān)鍵字是否在map中出現(xiàn))
在這里我們將體會,map在數(shù)據(jù)插入時保證有序的好處。
要判定一個數(shù)據(jù)(關(guān)鍵字)是否在map中出現(xiàn)的方法比較多,這里標(biāo)題雖然是數(shù)據(jù)的查找,在這里將穿插著大量的map基本用法。
?
這里給出三種數(shù)據(jù)查找方法
第一種:用count函數(shù)來判定關(guān)鍵字是否出現(xiàn),其缺點是無法定位數(shù)據(jù)出現(xiàn)位置,由于map的特性,一對一的映射關(guān)系,就決定了count函數(shù)的返回值只有兩個,要么是0,要么是1,出現(xiàn)的情況,當(dāng)然是返回1了
第二種:用find函數(shù)來定位數(shù)據(jù)出現(xiàn)位置,它返回的一個迭代器,當(dāng)數(shù)據(jù)出現(xiàn)時,它返回數(shù)據(jù)所在位置的迭代器,如果map中沒有要查找的數(shù)據(jù),它返回的迭代器等于end函數(shù)返回的迭代器,程序說明:
#include <map>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
?????? map<int,string> mymap;
?????? mymap.insert(map<int,string>::value_type(1,"student_one"));
?????? mymap.insert(map<int,string>::value_type(2,"student_two"));
?????? mymap.insert(map<int,string>::value_type(3,"student_three"));
?????? map<int,string>::iterator it;
?????? it=mymap.find(1);
?????? if(it!= mymap.end())
??????? {
?????? cout<< "Find, the value is " << it->second << endl;
??????? }
??????? else
??????? {
??????????? cout << "Do not Find" << endl;
??????? }
}
運行結(jié)果:
Find, the value is? student_one
第三種:這個方法用來判定數(shù)據(jù)是否出現(xiàn),是顯得笨了點,但是,我打算在這里講解
Lower_bound函數(shù)用法,這個函數(shù)用來返回要查找關(guān)鍵字的下界(是一個迭代器)
Upper_bound函數(shù)用法,這個函數(shù)用來返回要查找關(guān)鍵字的上界(是一個迭代器)
例如: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)這個關(guān)鍵字
(這個暫時還沒弄清楚。。。。。)
5.? 數(shù)據(jù)的清空與判空
清空map中的數(shù)據(jù)可以用clear()函數(shù),判定map中是否有數(shù)據(jù)可以用empty()函數(shù),它返回true則說明是空map
6.? 數(shù)據(jù)的刪除
這里要用到erase函數(shù),它有三個重載了的函數(shù),下面在例子中詳細說明它們的用法
#include <map>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
?????? map<int,string> mymap;
?????? mymap.insert(map<int,string>::value_type(1,"student_one"));
?????? mymap.insert(map<int,string>::value_type(2,"student_two"));
?????? mymap.insert(map<int,string>::value_type(3,"student_three"));
//如果你要演示輸出效果,請選擇以下的一種,你看到的效果會比較好
?
?????? //如果要刪除1,用迭代器刪除
?????? map<int, string>::iterator it;
?????? it = mymap.find(1);
?????? mymap.erase(iter);
?
?
?????? //如果要刪除1,用關(guān)鍵字刪除
?????? int n = mymap.erase(1);//如果刪除了會返回1,否則返回0
?
?
?????? //用迭代器,成片的刪除
?????? //一下代碼把整個map清空
?????? mymap.earse(mymap.begin(), mymap.end());
?????? //成片刪除要注意的是,也是STL的特性,刪除區(qū)間是一個前閉后開的集合
}
其余的STL—map功能暫不研究。
轉(zhuǎn)載于:https://www.cnblogs.com/ct0421/p/3718673.html
總結(jié)
以上是生活随笔為你收集整理的STL--map用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Excel中如何在打印时自动给每行加上标
- 下一篇: cron计划任务的介绍