日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++|STL学习笔记-map的属性(大小以及是否存在)

發布時間:2025/3/15 c/c++ 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++|STL学习笔记-map的属性(大小以及是否存在) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

1.size()的用法

2.多多使用count(xxx)進行判斷


?

1.size()的用法

map的property

map屬性
1.沒有容量;
2.得到元素的個數size()

?

這里給出調用他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(); }

運行截圖如下:

?

2.多多使用count(xxx)進行判斷

這里有個小知識點使用count判斷map是否存在,不存在返回0

個人感覺STL中map的這一點就沒有QTL好用了,

QTL是這樣的命名:

是不是感覺QTL更加通俗易懂:

下面給出STL中關于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(); }

運行截圖如下:

?

總結

以上是生活随笔為你收集整理的C++|STL学习笔记-map的属性(大小以及是否存在)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。