STL中的multimap---顺便说说如何查找同一关键字对应的所有值(利用count, lower_bound/upper_bound, equal_range)
来源:互联网 发布:淘宝网首页布局介绍 编辑:程序博客网 时间:2024/05/21 19:46
转自: http://blog.csdn.net/stpeace/article/details/44984639
我个人感觉哈, 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; } cout << "---------------------------" << 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()返回值
- Lintcode移动零
- Unity 进度条
- Python中的random模块(总结)
- 微信小程序例子——获取自己所处的位置坐标
- hdu 4472 Count(简单递推)
- STL中的multimap---顺便说说如何查找同一关键字对应的所有值(利用count, lower_bound/upper_bound, equal_range)
- C++ 基础知识四
- Angular 4.x 学习导引
- pc 电脑web浏览器js通过usb控制安卓手机打电话挂电话
- Codeforces 315C Sereja and Contest【思维】
- MySQL 忘记密码:skip-grant-tables
- Java并发编程 10 线程池
- 关于实模式下汇编伪指令assume的个人理解
- java定时schedule(task,time)