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:主要用来关闭 ...
随机推荐
- Android消息传递之EventBus 3.0
Android消息传递之EventBus 3.0使用详解 http://www.cnblogs.com/whoislcj/p/5595714.html EventBus 3.0进阶:源码及其设计模式 ...
- vue入门(三)----使用vue-cli搭建一个单页富应用
上面两节我们说了vue的一些概念,其实说的知识一点基础,这部分知识我觉得更希望大家到官网进行学习,因为在这里说的太多我觉得也只是对官网的照搬照抄而已.今天我们来学习一下vue-cli的一些基础知识,并 ...
- C#提取TXT文档指定内容
早上有分享一篇<VB.NET提取TXT文档指定内容> http://www.cnblogs.com/insus/p/3267347.html 那是原网友的需求用VB.NET写的.刚才有只懂 ...
- html5 语音识别 转
- 14.链表中倒数第k个节点
题目描述: 输入一个链表,输出该链表中倒数第k个结点. 思路分析: 设置两个指针,一个fast一个slow,都从链表头开始,让fast先走k步,然后两个指针一起走,当fast走到尾部,那么sl ...
- Bootstrap FileInput 上传 中文 API 整理
Bootstrap FileInput 上传 中文 API 整理 上传插件有很多 但是公司用的就是 Bootstrap FileInput 自己就看了看 会用就行 自己都不知道每个值是干嘛用的就问 ...
- Applese 涂颜色(python解法)
链接:https://ac.nowcoder.com/acm/contest/330/E 来源:牛客网 题目描述 精通程序设计的 Applese 叕写了一个游戏. 在这个游戏中,有一个 n 行 m 列 ...
- 华东交通大学2015年ACM“双基”程序设计竞赛1004
Problem D Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Sub ...
- mc04_IntelliJ IDEA常用设置
字体设置 File --> Settings --> Font 项目编码设置 File --> Settings --> File Encodings 项目依赖 即一个项目引用 ...
- php数组·的方法-数组与数据结构
/*数组与数据结构*/ //shuffle() 随机打乱数组 //array_push() 数组末尾添加元素 //array_pop() 数组末尾删除元素 //array_shift() 数组首位删除 ...