STL中的multimap---顺便说说如何查找同一关键字对应的所有值(利用count, lower_bound/upper_bound, equal_range)
来源:互联网 发布:淘宝推广技巧视频 编辑:程序博客网 时间:2024/06/08 11:27
我个人感觉哈, map的应用场景比multimap更多, 不过, 我们还是来学一下multimap。 我们知道, multimap中, 一个关键字可能对应多个不同的值, 怎么获取呢?我们来看程序, 接招(介绍三种方法):
#pragma warning(disable : 4786)#include <map>#include <string>#include <iostream>using namespace std;int main(){multimap<int, string> mp;mp.insert(pair<int, string>(3, "hehe"));mp.insert(pair<int, string>(4, "haha"));mp.insert(pair<int, string>(2, "error"));mp.insert(pair<int, string>(3, "good"));mp.insert(pair<int, string>(3, "ok"));mp.insert(pair<int, string>(3, "hehe"));multimap<int, string>::iterator it;for(it = mp.begin(); it != mp.end(); it++){cout << it->first << "--->";cout << it->second << endl;}// 方法一int n = mp.count(3); // 3的个数cout << n << endl;int i = 0;it = mp.find(3); // 第一个3的位置for(i = 0; i < n; i++){cout << it->first << "--->";cout << it->second << endl;it++; // 所有的3必然是相连的}cout << "---------------------------" << endl;// 方法二:for(it = mp.lower_bound(3); it != mp.upper_bound(3); it++){cout << it->first << "--->";cout << it->second << endl;}cout << "---------------------------" << endl;// 方法三:pair<multimap<int, string>::iterator, multimap<int, string>::iterator > pos;for(pos = mp.equal_range(3); pos.first != pos.second; pos.first++){cout << pos.first->first << "--->";cout << pos.first->second << endl;}return 0;}结果为:
2--->error
3--->hehe
3--->good
3--->ok
3--->hehe
4--->haha
4
3--->hehe
3--->good
3--->ok
3--->hehe
---------------------------
3--->hehe
3--->good
3--->ok
3--->hehe
---------------------------
3--->hehe
3--->good
3--->ok
3--->hehe
Press any key to continue
好吧, 先这样。
0 0
- STL中的multimap---顺便说说如何查找同一关键字对应的所有值(利用count, lower_bound/upper_bound, equal_range)
- STL中的multimap---顺便说说如何查找同一关键字对应的所有值(利用count, lower_bound/upper_bound, equal_range)
- STL中的multimap---顺便说说如何查找同一关键字对应的所有值
- stl algorithm : count、find、lower_bound、upper_bound和equal_range的区别
- multimap 操作lower_bound、 upper_bound、 equal_range
- STL中的二分查找算法(binary_search lower_bound upper_bound equal_range)
- multimap使用lower_bound、 upper_bound、 equal_range进行特定key的遍历
- STL algorithm之count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- STL algorithm之count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- STL algorithm之count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- 注意count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- 注意count,find,binary_seach,lower_bound,upper_bound和equal_range的区别
- count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- STL中的四个二分查找算法(binary_search lower_bound upper_bound equal_range)
- C++ STL 算法:查找算法(7) lower_bound、upper_bound、equal_range
- C++STL算法lower_bound、upper_bound、equal_range
- effective stl 第45条:正确区分count/find/binary_search/lower_bound/upper_bound/equal_range
- equal_range()、lower_bound() 和 upper_bound()返回值
- 2014 UESTC Training for Data Structures——J 方师傅的01串
- 点击天数分析
- Leetcode4: Factorial Trailing Zeroes
- c语言socket使用http协议访问apache服务器
- CentOS6.5安装Tomcat7.0.29
- STL中的multimap---顺便说说如何查找同一关键字对应的所有值(利用count, lower_bound/upper_bound, equal_range)
- Search Insert Position -LeetCode
- 黄金分割数
- Mongodb Linux安装
- hdu1847Good Luck in CET-4 Everybody! 博弈
- ZOJ 1409 Communication System(枚举 + 贪心)
- 神奇算式
- POJ 1046 Color Me Less
- 一个新的开始?