c++关于map的find和count的使用

本文介绍了C++标准库中map容器的find和count方法的使用方式。find方法用于定位指定键值的元素位置,若不存在则返回end迭代器;count方法返回指定键值的元素数量,因map不允许重复键值,故结果为0或1。

c++关于map的find和count的使用 http://www.cnblogs.com/Deribs4/p/4948351.html

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

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

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

例子:

复制代码
 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 }
复制代码

 

运行结果:

标签: c++, map
C++标准模板库(STL)中,`find``count`是两种常用的关联容器操作,它们在功能性能上各有特点。以下是对这两个函数执行效率的比较分析: ### 功能区别 - `find`函数用于查找指定键值的元素,如果找到则返回指向该元素的迭代器,否则返回指向容器末尾的迭代器。 - `count`函数用于统计指定键值的元素数量。对于`std::map``std::set`等不允许多个相同键值的容器,`count`的结果只能是0或1。 ### 时间复杂度 - 对于`std::map``std::set`等基于红黑树实现的关联容器,`find``count`的时间复杂度均为O(log n),因为它们都需要进行二分查找。 - 对于`std::unordered_map``std::unordered_set`等基于哈希表实现的无序关联容器,`find``count`的时间复杂度为O(1)(平均情况下),但在最坏情况下可能退化为O(n)。 ### 性能比较 - **查找操作**:`find`函数在找到目标元素后会立即返回,而`count`函数则需要遍历整个容器以统计所有匹配的元素数量。因此,在只需要判断元素是否存在的情况下,`find`通常比`count`更高效。 - **内存访问模式**:`find`函数的内存访问模式更为局部化,因为它一旦找到目标元素即可停止搜索。相比之下,`count`函数需要遍历整个容器,导致更多的内存访问更高的缓存不命中率。 - **哈希冲突**:在`std::unordered_map``std::unordered_set`中,如果存在大量的哈希冲突,`count`的性能可能会显著下降,因为它需要检查每个冲突的元素。 ### 使用建议 - 如果只需要判断某个键值是否存在,推荐使用`find`函数,因为它可以在找到第一个匹配项后立即返回。 - 如果需要统计某个键值出现的次数,尤其是在允许多个相同键值的容器(如`std::multimap``std::multiset`)中,推荐使用`count`函数。 ### 示例代码 以下是一个简单的示例,展示了如何在`std::map`中使用`find``count`: ```cpp #include <iostream> #include <map> int main() { std::map<int, std::string> myMap; myMap[1] = "one"; myMap[2] = "two"; myMap[3] = "three"; // 使用 find 查找元素 auto it = myMap.find(2); if (it != myMap.end()) { std::cout << "Found: " << it->second << std::endl; } else { std::cout << "Not found" << std::endl; } // 使用 count 统计元素数量 int count = myMap.count(2); std::cout << "Count: " << count << std::endl; return 0; } ``` ### 结论 综上所述,`find``count`在不同的应用场景下各有优劣。选择合适的函数可以提高程序的性能可读性[^1]。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值