C++|STL学习笔记-map的属性(大小以及是否存在)
生活随笔
收集整理的這篇文章主要介紹了
C++|STL学习笔记-map的属性(大小以及是否存在)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
1.size()的用法
2.多多使用count(xxx)進(jìn)行判斷
?
1.size()的用法
map的property
map屬性
1.沒有容量;
2.得到元素的個(gè)數(shù)size()
?
這里給出調(diào)用他size()的例子,源碼如下:
/************************************************************************/ /* map property */ /************************************************************************/#include <map> #include <iostream> #include <algorithm> using namespace std;typedef pair<int, char> in_pair; typedef pair<map<int, char>::iterator, bool> in_pair_bool;void judgeOk(in_pair_bool pr){if(pr.second){cout << "insert the success!" << endl;}else{cout << "insert the failture!" << endl;} }void mapProperty(){map<int, char> mp;pair<map<int, char>::iterator, bool> pr;pr = mp.insert(in_pair(1, 'a'));judgeOk(pr);pr = mp.insert(in_pair(2, 'b'));judgeOk(pr);pr = mp.insert(in_pair(3, 'c'));judgeOk(pr);pr = mp.insert(in_pair(4, 'd'));judgeOk(pr);pr = mp.insert(in_pair(2, 'e'));judgeOk(pr);cout << "The map size is " << mp.size() << endl; }void main(){mapProperty();getchar(); }運(yùn)行截圖如下:
?
2.多多使用count(xxx)進(jìn)行判斷
這里有個(gè)小知識(shí)點(diǎn)使用count判斷map是否存在,不存在返回0
個(gè)人感覺STL中map的這一點(diǎn)就沒有QTL好用了,
QTL是這樣的命名:
是不是感覺QTL更加通俗易懂:
下面給出STL中關(guān)于count的栗子:
/************************************************************************/ /* map property */ /************************************************************************/#include <map> #include <iostream> #include <algorithm> using namespace std;typedef pair<int, char> in_pair; typedef pair<map<int, char>::iterator, bool> in_pair_bool;void judgeOk(in_pair_bool pr){if(pr.second){cout << "insert the success!" << endl;}else{cout << "insert the failture!" << endl;} }void mapProperty(){map<int, char> mp;pair<map<int, char>::iterator, bool> pr;pr = mp.insert(in_pair(1, 'a'));judgeOk(pr);pr = mp.insert(in_pair(2, 'b'));judgeOk(pr);pr = mp.insert(in_pair(3, 'c'));judgeOk(pr);pr = mp.insert(in_pair(4, 'd'));judgeOk(pr);pr = mp.insert(in_pair(2, 'e'));judgeOk(pr);cout << "The map size is " << mp.size() << endl;cout << "The use of count() function" << endl;if(mp.count(3)){cout << "presence key three" << endl;}map<int, char>::iterator it = mp.begin();for(it; it != mp.end(); it++){cout << it->first << "\t" << it->second << endl;}}void main(){mapProperty();getchar(); }運(yùn)行截圖如下:
?
總結(jié)
以上是生活随笔為你收集整理的C++|STL学习笔记-map的属性(大小以及是否存在)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ opengl 环境光分量
- 下一篇: Qt工作笔记-对主事件循环的进一步认识