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:主要用来关闭 ...
随机推荐
- EIP权限工作流升级说明-2019/3/5
首页增加待办事项直接处理按钮 2,新增处理历史记录
- [Algorithm]栈和队列
一.栈和队列综合(算法) 1.判断单链表(带头结点)的结点值(字符型)是否中心对称 1 bool IsSymmetry( LinkList& L ) 2 { 3 char S[MAXSIZE] ...
- Linux_Shell_ Map 的使用和遍历
定义初始化map declare -A map=([") 输出所有key echo ${map[@]} 输出key对应的值 "]} 遍历map for key in ${!map[ ...
- redis-淘汰策略
将redis用作缓存时,如果内存空间用满,就会自动驱逐老的数据.默认情况下,memcached就是这种方式. LRU是Redis唯一支持的回收算法. maxmemory配置指令 maxmemory用于 ...
- loj#6040. 「雅礼集训 2017 Day5」矩阵(线性代数+递推)
题面 传送门 题解 我的线代学得跟屎一样看题解跟看天书一样所以不要指望这题我会写题解 这里 //minamoto #include<bits/stdc++.h> #define R reg ...
- P5212 SubString LCT+SAM
$ \color{#0066ff}{ 题目描述 }$ 给定一个字符串init,要求支持两个操作 在当前字符串的后面插入一个字符串 询问字符串ss在当前字符串中出现了几次?(作为连续子串) 强制在线. ...
- P3240 [HNOI2015]实验比较 树形DP
\(\color{#0066ff}{ 题目描述 }\) 小D 被邀请到实验室,做一个跟图片质量评价相关的主观实验.实验用到的图片集一共有 \(N\) 张图片,编号为 \(1\) 到\(N\).实验分若 ...
- [BZOJ3337] ORZJRY I --块状链表大毒瘤
link 题目大意:维护一个序列 支持: 1.单点插入 2.单点删除 3.区间翻转 4.区间旋转 5.区间加 6.区间赋值 7.询问区间和 8.询问区间极差 9.询问区间与给定某个数差值绝对值的最小值 ...
- AtCoder Beginner Contest 120 题解
题目链接:https://atcoder.jp/contests/abc120 A Favorite Sound 分析:答案为. 代码: #include <iostream> using ...
- elasticsearch 相关命令
ubuntu server 启动命令:$ ./bin/elasticsearch 后台运行:$ ./bin/elasticsearch -d 查看当前进程:$ jps 启动后检查是否成功命令:$ cu ...