c++关于map的find和count的使用
来源:互联网 发布:lol淘宝代练价格表 编辑:程序博客网 时间:2024/06/06 10:56
c++关于map的find和count的使用 http://www.cnblogs.com/Deribs4/p/4948351.html
编程的时候比较常用,今天记录一下,以后备用。
使用count,返回的是被查找元素的个数。如果有,返回1;否则,返回0。注意,map中不存在相同元素,所以返回值只能是1或0。
使用find,返回的是被查找元素的位置,没有则返回map.end()。
例子:
1 #include<string> 2 #include<cstring> 3 #include<iostream> 4 #include<queue> 5 #include<map> 6 #include<algorithm> 7 using namespace std; 8 int main(){ 9 map<string,int> test;10 test.insert(make_pair("test1",1));//test["test1"]=111 test.insert(make_pair("test2",2));//test["test2"]=212 map<string,int>::iterator it;13 it=test.find("test0");14 cout<<"test0 find:";15 if(it==test.end()){16 cout<<"test0 not found"<<endl;17 }18 else{19 cout<<it->second<<endl;20 }21 cout<<"test0 count:";22 cout<<test.count("test1")<<endl;23 24 cout<<"test1 find:";25 it=test.find("test1");26 if(it==test.end()){27 cout<<"test1 not found"<<endl;28 }29 else{30 cout<<it->second<<endl;31 }32 cout<<"test1 count:";33 cout<<test.count("test1")<<endl;34 35 cout<<"after inserting test1"<<endl;36 test.insert(make_pair("test1",2));37 cout<<"test1 find:";38 it=test.find("test1");39 if(it==test.end()){40 cout<<"test1 not found"<<endl;41 }42 else{43 cout<<it->second<<endl;44 }45 cout<<"test1 count:";46 cout<<test.count("test1")<<endl;47 return 0;48 }
运行结果:
标签: c++, map
阅读全文
0 0
- c++关于map的find和count的使用
- c++ map.count 和map.find 的用法区别
- 关于map的count和find的效率以及空类调用效率(一)
- 关于map的count和find的效率以及空类调用效率(一)
- map count的使用
- 关于std::map中的find和[]的问题
- 关于std::map中的find和[]的问题
- 关于std::map中的find和[]的问题
- 关于std::map中的find和[]的问题
- 【转载】关于std::map中的find和[]的问题
- unordered_set的find,count效率 和赋值
- 关于C++map的简单使用
- oracle中关于count(1)、count(*)、count(rowid)、count(某个字段)使用上的区别和性能问题
- oracle中关于count(1)、count(*)、count(rowid)、count(某个字段)使用上的区别和性能问题
- Uva156 map容器的使用 sortvecor And map.count
- 关于map的使用
- C++ count和count_if的使用
- C++map的基本操作和使用
- 第1.4章 scrapy之setting
- xilinx SDK中:找不到include xgpio.h的解决办法
- 需要看的教程(更新中)
- CSS字符串截取
- PHP实现接口多继承
- c++关于map的find和count的使用
- Spring之装配bean(二)
- Android5.0系统wifip2p默认ssid和密码
- aqtest 003
- mac OX下jenkins安装配置
- how to calculate receptive field of CNN
- 友盟的社会化分享和第三方登录集成步骤
- oracle数据库回车换行的转换
- c语言面试题