编程的时候比较常用,今天记录一下,以后备用。

使用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的使用的更多相关文章

  1. 在eclipse使用map reduce编写word count程序生成jar包并在虚拟机运行的步骤

    ---恢复内容开始--- 1.首先准备一个需要统计的单词文件 word.txt,我们的单词是以空格分开的,统计时按照空格分隔即可 hello hadoop hello yarnhello zookee ...

  2. 【Map】获取字符串中,每一个字母出现的次数

    package cn.itcast.p1.map.test; import java.util.Iterator; import java.util.Map; import java.util.Tre ...

  3. Map的基本用法(Java)

    package home.collection.arr; import java.awt.Window.Type; import java.util.ArrayList; import java.ut ...

  4. List<Map<String,String>>操作(遍历,比较)

    1.List<Map<String,String>>的遍历: Map<String,String> map = new HashMap<String, Str ...

  5. 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 ...

  6. LeetCode算法题-Subdomain Visit Count(Java实现)

    这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...

  7. mybatis xml中返回map 参看aiwanpai

    <!-- 指定日期活动被创建次数查询结果数据集--> <resultMap id="countPlayTimesMap" type="HashMap&q ...

  8. 12:集合map、工具类

    一.map集合 Map:一次添加一对元素.Collection 一次添加一个元素. Map也称为双列集合,Collection集合称为单列集合. 其实map集合中存储的就是键值对(结婚证书), map ...

  9. Go语言学习之4 递归&闭包&数组切片&map&锁

    主要内容: 1. 内置函数.递归函数.闭包2. 数组与切片3. map数据结构4. package介绍 5. 排序相关 1. 内置函数.递归函数.闭包 1)内置函数 (1). close:主要用来关闭 ...

随机推荐

  1. SqlServer压缩数据库日志

    )--数据库名称 )--数据库日志文件名称 --替换成自己的文件名称 select @dbName='dbname' select @dbNamelog='dbname_log' ) set @sql ...

  2. .Net Core异步async/await探索

    走进.NetCore的异步编程 - 探索 async/await 前言: 这段时间开始用.netcore做公司项目,发现前辈搭的框架通篇运用了异步编程方式,也就是async/await方式,作为一个刚 ...

  3. Exception in thread "main" brut.androlib.err.UndefinedResObject: resource spec: 0x01030200

    Exception in thread "main" brut.androlib.err.UndefinedResObject: resource spec: 0x01030200 ...

  4. C# console application executing macro function

    C#控制台应用程序,执行或运行Office的宏函数,程序如下: 应用例子:

  5. Kotlin 数据类型(数值类型)

    Kotlin 的常见数据类型: 类型 范围 byte -128~127 short 32767-32768 int -2147483648~2147483647 long 92233720368547 ...

  6. Git入门操作

    仅学习Git的一些入门操作比较容易,平时更多地使用GitHub,不过今天我想自个搭个服务练练手.当看完一些材料合作了一些验证之后,才发现其实所谓的服务和之前的svn完全不一样了.过程记录如下: Lin ...

  7. 算法训练 Balloons in a Box (枚举,模拟)

    问题描述 你要写一个程序,使得能够模拟在长方体的盒子里放置球形的气球. 接下来是模拟的方案.假设你已知一个长方体的盒子和一个点集.每一个点代表一个可以放置气球的位置.在一个点上放置一个气球,就是以这个 ...

  8. oracle数据结构

    数据类型: 1 字符数据:CHAR VARCHAR NCHAR NVARCHAR2 LONG CLOB NCLOB  2 数字数据类型:NUMBER 唯一用来存储数字型的类型 3 日期数据类型: 4 ...

  9. Oracle中merge into语法

    merge into 语句就是insert和update的一个封装,简单来说就是: 有则更新,无则插入 下面说怎么使用 MERGE INTO table_Name  T1(匿名) using (另外一 ...

  10. thrift 通信的使用 /安装

    参考: http://blog.csdn.net/yohunl/article/details/41748511 http://blog.csdn.net/amuseme_lu/article/det ...