c++关于map的find和count的使用
编程的时候比较常用,今天记录一下,以后备用。
使用count,返回的是被查找元素的个数。如果有,返回1;否则,返回0。注意,map中不存在相同元素,所以返回值只能是1或0。
使用find,返回的是被查找元素的位置,没有则返回map.end()。
例子:
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
int main(){
map<string,int> test;
test.insert(make_pair("test1",));//test["test1"]=1
test.insert(make_pair("test2",));//test["test2"]=2
map<string,int>::iterator it;
it=test.find("test0");
cout<<"test0 find:";
if(it==test.end()){
cout<<"test0 not found"<<endl;
}
else{
cout<<it->second<<endl;
}
cout<<"test0 count:";
cout<<test.count("test1")<<endl; cout<<"test1 find:";
it=test.find("test1");
if(it==test.end()){
cout<<"test1 not found"<<endl;
}
else{
cout<<it->second<<endl;
}
cout<<"test1 count:";
cout<<test.count("test1")<<endl; cout<<"after inserting test1"<<endl;
test.insert(make_pair("test1",));
cout<<"test1 find:";
it=test.find("test1");
if(it==test.end()){
cout<<"test1 not found"<<endl;
}
else{
cout<<it->second<<endl;
}
cout<<"test1 count:";
cout<<test.count("test1")<<endl;
return ;
}
运行结果:
c++关于map的find和count的使用的更多相关文章
- 在eclipse使用map reduce编写word count程序生成jar包并在虚拟机运行的步骤
---恢复内容开始--- 1.首先准备一个需要统计的单词文件 word.txt,我们的单词是以空格分开的,统计时按照空格分隔即可 hello hadoop hello yarnhello zookee ...
- 【Map】获取字符串中,每一个字母出现的次数
package cn.itcast.p1.map.test; import java.util.Iterator; import java.util.Map; import java.util.Tre ...
- Map的基本用法(Java)
package home.collection.arr; import java.awt.Window.Type; import java.util.ArrayList; import java.ut ...
- List<Map<String,String>>操作(遍历,比较)
1.List<Map<String,String>>的遍历: Map<String,String> map = new HashMap<String, Str ...
- mybatis mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map
用mybaits 写一个关联查询 查询商品表关联商品规格表,并查询规格表中的数量.价格等,为了sql重用性,利用 association 节点 查询 结果并赋值报错 商品表的mapper文件为Gooo ...
- LeetCode算法题-Subdomain Visit Count(Java实现)
这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...
- mybatis xml中返回map 参看aiwanpai
<!-- 指定日期活动被创建次数查询结果数据集--> <resultMap id="countPlayTimesMap" type="HashMap&q ...
- 12:集合map、工具类
一.map集合 Map:一次添加一对元素.Collection 一次添加一个元素. Map也称为双列集合,Collection集合称为单列集合. 其实map集合中存储的就是键值对(结婚证书), map ...
- Go语言学习之4 递归&闭包&数组切片&map&锁
主要内容: 1. 内置函数.递归函数.闭包2. 数组与切片3. map数据结构4. package介绍 5. 排序相关 1. 内置函数.递归函数.闭包 1)内置函数 (1). close:主要用来关闭 ...
随机推荐
- 判断本地是否存在Jquery文件,如果不存在则使用CDN加速的Jquery文件
<script>//判断是否成功将Jquery库引入,如果没有成功引入则引入本地Jquery库if (typeof jQuery == 'undefined') {document.wri ...
- 关于Linq对DataTable和List各自的两个集合筛选的相关操作技巧
项目中用到了对两个集合的帅选等操作,简单总结下 1.Linq操作多个Datable 可以通过AsEnumerable()方法对DataTable进行Linq操作 //获取数据表所有数据 DataTab ...
- java 学习第三篇if判断
JAVA 判断 单词: if 如果 else 否则 单分支: If(条件) { 代码块 } If是一个判断语句.代码格式如上. If括号的内是表达式.如果表达式值是成立的便执行代码块.之后在执行IF语 ...
- 数组谓词查询法 NSPredicate
NSPredicate:谓词 字面翻译是这个意思,但是我觉得谓词这个词太难以理解了 NSPredicate的具体用途应该还是过滤,类似于过滤条件之类的,相当于一个主语的谓语,所以说会是谓词这个名字.( ...
- Beautiful Sequence
Beautiful Sequence 给定一些数(可能相同),将它们随机打乱后构成凹函数,求概率 .N<=60 . 首先,这种题求概率事实上就是求方案.所以现在要求的是用这些数构成凹函数的方案数 ...
- [Leetcode]014. Longest Common Prefix
public class Solution { public String longestCommonPrefix(String[] strs) { if(strs == null || strs.l ...
- #333 Div2 Problem B Approximating a Constant Range(尺取法)
题目:http://codeforces.com/contest/602/problem/B 题意 :给出一个含有 n 个数的区间,要求找出一个最大的连续子区间使得这个子区间的最大值和最小值的差值不超 ...
- 10.Spring集成一
1.Email 电子邮件工作原理: 邮件发送方通过邮件发送客户端把邮件发送到发送方的邮件服务器,在发送的过程中需要用到SMTP协议发送方的邮件服务器把邮件发送到接收方的邮件服务器,使用的协议也是SMT ...
- windows服务开启(收藏url)
windows服务开启(收藏url) http://blog.csdn.net/wanda39kela/article/details/46310093
- java spi机制和场景
JDK中查找服务的实现的工具类是:java.util.ServiceLoader, jvm启动时自动查找META-INF/services/下面的实现类,并初始化实例 jdbc的mysql实现类 在静 ...