c++ map之中find和count函数的区别

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

使用count,返回的是被查找元素的个数。如果有,返回1;否则,返回0。注意,map中不存在相同元素,所以返回值只能是1或0。

使用find,返回的是被查找元素的位置,没有则返回map.end()。

find的函数值是一个iter 结构体,count是数值

例子:

 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"]=1
11     test.insert(make_pair("test2",2));//test["test2"]=2
12     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 }

运行结果:

时间: 2024-12-13 08:17:41

c++ map之中find和count函数的区别的相关文章

STL map详细用法和make_pair函数

今天练习华为上机测试题,遇到了map的用法,看来博客http://blog.csdn.net/sprintfwater/article/details/8765034:感觉很详细,博主的其他内容也值得学习:后面附上今天的练习题目. 首先make_pair Pairs C++标准程序库中凡是“必须返回两个值”的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和multimap就是使用pairs来管理其健值/实值(key/va lue)的成对元素. pai

使用python实现内置map,filter,reduce函数

map函数 # -*- coding: cp936 -*- def myselfmap(f,*args):     def urgymap(f,args):         if args==[]:             return []         else:             return [f(args[0])]+urgymap(f,args[1:])     if list(args)[0]==[]:             #*args有多个参数被传递时返回tuple  

如何定位Release 版本中程序崩溃的位置 ---利用map文件 拦截windows崩溃函数

1       案例描述 作为Windows程序员,平时最担心见到的事情可能就是程序发生了崩溃(异常),这时Windows会提示该程序执行了非法操作,即将关闭.请与您的供应商联系.呵呵,这句微软的“名言”,恐怕是程序员最怕见也最常见的东西了. 在一个大型软件的测试过程中,初期出现程序崩溃似乎成了不可避免的事.其实测试中出现程序崩溃并不可怕,反而是测试的成功.作为开发的我们更需要关心的是程序中的哪个函数或哪一行导致了系统崩溃,这样才能有针对性的进行改正. 本文描述了自己总结的几种定位崩溃的办法.

map 和 vector 的erase函数说明

1. map的erase函数使用 这里首先要注意,C++针对map的erase函数有不同的函数原型,这往往是出现问题的关键所在.根据参考文献1: 在C++98中: ? 1 2 3 4 5 (1) void erase (iterator position); (2)size_type erase (const key_type& k); (3)void erase (iterator first, iterator last); 在C++11中: ? 1 2 3 4 5 (1)iterator 

jquery中map函数与each函数的区别

?jquery中的each函数和map函数的用法看起来差不多,但其实还是有一点区别的. ?其中一个重要的区别是,each返回的是原来的数组,并不会新创建一个数组.而map方法会返回一个新的数组.如果在没有必要的情况下使用map,则有可能造成内存浪费. ?例如: var items = [1,2,3,4]; ? $.each(items, function() { alert('this is ' + this); }); var newItems = $.map(items, function(

php count()函数用法 及其 一个坑

用法 count() 函数返回数组中元素的数目. count(array,mode); [mode] 0 - 默认.不计算多维数组中的所有元素. 1 - 递归地计算数组中元素的数目(计算多维数组中的所有元素). 坑 单纯的判断count($array)>0来判断数组是否存在是不严谨的因为有可能 $array = '';echo (count($array)); 输出1;

TSql Count 函数三种写法介绍

Count常见的有三种写法,count(*),count(expression),count(column_name) Count(expression) ,count(column_name)计数的机制是:计算 expression,或表中column_name的值是否为NULL,如果为NULL则不计数,如果不是NULL则会计数. count(*),返回表中行的数目.Specifies that all rows should be counted to return the total nu

【SAS ADVANCE】通过COUNT函数实现counting values及HAVING子句

一. COUNT函数 在实际统计应用中,需要计算观测值出现的次数,这项功能可以通过COUNT函数来实现,它有下面三种形式和功能: form you used 返回值 例子 COUNT(*) 整个表或组的行数   select count(*) as Count COUNT(column) 当参数中所选定的列没有缺失值时,输出表或者组中该变量的行数   select count(jobcode) as Count COUNT(DISTINCT column) 该列中distinct values的

php count函数

最近被问到一个函数count 1.count("123456") 对应的输出是什么? 2.count(null) 对应的输出是什么? 以前没有认真的考虑,只是心里有一个印象那就是count()函数是计算php数组的个数的.后来下来仔细看了看手册,原来发现: 1.count函数是统计数组里所有元素的个数或者对象的属性的个数的,函数原型如下: int count(mixed $var[, $mode=COUNT_NORMAL); 对于对象,如果安装了SPL,可以通过Countable接口调