C++|STL学习笔记-map的属性(大小以及是否存在)
发布日期:2021-06-30 10:57:39 浏览次数:2 分类:技术文章

本文共 2254 字,大约阅读时间需要 7 分钟。

目录


 

1.size()的用法

map的property

map属性

1.没有容量;
2.得到元素的个数size()

 

这里给出调用他size()的例子,源码如下:

/************************************************************************//* map property                                                         *//************************************************************************/#include #include 
#include
using namespace std;typedef pair
in_pair;typedef pair
::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
mp; pair
::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 #include 
#include
using namespace std;typedef pair
in_pair;typedef pair
::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
mp; pair
::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
::iterator it = mp.begin(); for(it; it != mp.end(); it++){ cout << it->first << "\t" << it->second << endl; }}void main(){ mapProperty(); getchar();}

运行截图如下:

 

转载地址:https://it1995.blog.csdn.net/article/details/84132336 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Linux学习笔记-grep的基本认识
下一篇:linux工作笔记-linux之间文件传输图形界面工具gftp

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月24日 00时15分44秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章