C++|STL学习笔记-map的属性(大小以及是否存在)
发布日期:2021-06-30 10:57:39
浏览次数:2
分类:技术文章
本文共 2254 字,大约阅读时间需要 7 分钟。
目录
1.size()的用法
map的property
map属性
1.没有容量; 2.得到元素的个数size()
这里给出调用他size()的例子,源码如下:
/************************************************************************//* map property *//************************************************************************/#include
运行截图如下:
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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月24日 00时15分44秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
android内容提供者
2019-04-30
java异常知识点总结
2019-04-30
C++ 虚函数表
2019-04-30
C++ 在栈上分配内存
2019-04-30
boost::shared_array
2019-04-30
浅谈C++中指针和引用的区别
2019-04-30
C++学习笔记
2019-04-30
XlFileFormat 枚举 (Excel)
2019-04-30
SECP256K1签名
2019-04-30
WINDOWS匿名管道
2019-04-30
非模式对话框的问题
2019-04-30
C++解析XML, TINYXML2的使用
2019-04-30
Python环境搭建
2019-04-30
python pip及安装包安装
2019-04-30
openssl使用
2019-04-30
集线程池应用、多次HttpWebRequest请求,自动切换账号等等的移动信息查询软件
2019-04-30
HTTP协议状态码详解(HTTP Status Code)
2019-04-30
css优先级
2019-04-30
收集各种浏览器的Hack写法
2019-04-30
CSS选择器笔记
2019-04-30