经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
c/c++ 标准库 map multimap元素访问
来源:cnblogs  作者:小石王  时间:2018/9/26 18:01:12  对本文有异议

标准库 map multimap元素访问

一,map,unordered_map下标操作

下标操作种类 功能描述
c[k] 返回关键字为k的元素;如果k不在c中,添加一个关键字为k的元素,并对其初始化
c.at(k) 访问关键字为k的元素;若k不在c中,抛出out_of_range异常

注意:

1,当使用使用自定义类作为key时,这个类必须重写operator<函数

2,下标操作只适用于const map,unordered_map

二,访问元素

查找元素的操作 功能描述
c.find(k) 返回一个迭代器,指向第一个关键字为k的元素,若k不在c种,则返回c.end()
c.count(k) 返回关键字等于k的元素的数量。
c.lower_bound(k) 返回一个迭代器,指向第一个关键字大于等于k的元素。若k不在c中,返回和c.upper_bound(k)相等的迭代器。
c.upper_bound(k) 返回一个迭代器,指向第一个关键字大于k的元素。若k不在c中,返回和c.lower_bound(k)相等的迭代器。
c.equal_range(k) 返回一个pair,pair里面是2个c的迭代器。first为第一个关键字等于k的迭代器,second为最后一个关键字等于k的位置的下一个位置的迭代器。若未找到,则pair的2个成员都等于c.end()

小例子向导:

程序块 功能描述
test1 map的下标操作
test2 map 用自定义类型的下标操作
test3 map的查找
test4 multimap的查找

小例子:

  1. #include <iostream>
  2. #include <map>
  3. #include <unordered_map>
  4. #include <set>
  5. #include <vector>
  6. using namespace std;
  7. class Test{
  8. public:
  9. Test(int d = 0):data(d){}
  10. bool operator<(const Test& s)const{
  11. return s.data < data;
  12. }
  13. const int& getData()const{
  14. return data;
  15. }
  16. private:
  17. int data;
  18. };
  19. int main(){
  20. //test1 map的下标操作
  21. /*
  22. map<string,int> smap{{"aa",12},{"bb",10}};
  23. unordered_map<int, int> imap{{1,11},{2,22}};
  24. map<string,int>::mapped_type m1 = smap["aa"];//m1为int
  25. cout << m1 << endl;
  26. unordered_map<string,int>::mapped_type m2 = imap[2];//m2为int
  27. cout << m2 << endl;
  28. smap["aa"] = 33;
  29. cout << smap["aa"] << endl;
  30. smap["cc"] = 13;//想smap添加{"cc",13}
  31. cout << smap["cc"] << endl;
  32. cout << smap.at("cc") << endl;
  33. //cout << smap.at("ccd") << endl;//抛出out_of_range异常
  34. map<string,int>::mapped_type m3 = smap.at("aa");
  35. cout << m3 << endl;
  36. //想smap里添加了{"dd", 0},
  37. cout << smap["dd"] << endl;
  38. for(auto const &s : smap){
  39. cout << s.first << "," << s.second << endl;
  40. }
  41. */
  42. //test2 map 用自定义类型的下标操作
  43. /*
  44. map<Test,int> tmap{{Test(10), 10},{Test(11), 11}};
  45. tmap[Test()] = 1;
  46. for(auto const &s : tmap){
  47. cout << s.first.getData() << "," << s.second << endl;
  48. }
  49. */
  50. //test3 map的查找
  51. /*
  52. map<int, int> imap{{1,1},{3,3},{2,2},{5,5},{4,4}};
  53. map<int,int>::iterator it1 = imap.find(1);
  54. cout << it1->first << endl;
  55. map<int,int>::iterator it2 = imap.find(4);//返回imap.end()
  56. if(it2 == imap.end()){cout << "it2 is end" << endl;}
  57. cout << imap.count(2) << endl;
  58. auto it3 = imap.lower_bound(2);//{2,2}
  59. cout << it3->first << "," << it3->second << endl;
  60. auto it4 = imap.upper_bound(4);//{5,5}
  61. cout << it4->first << "," << it4->second << endl;
  62. */
  63. //test4 multimap的查找
  64. multimap<string, string> autrs{{"aaa","n1"},{"bbb","n1"},{"aaa","n2"},
  65. {"aaa","n3"}};
  66. string sch("aaa");
  67. //方法1
  68. auto cnt = autrs.count(sch);
  69. auto it = autrs.find(sch);
  70. while(cnt){
  71. cout << it->second << endl;
  72. ++it;
  73. --cnt;
  74. }
  75. cout << "-----------------" << endl;
  76. //方法2
  77. for(auto beg = autrs.lower_bound(sch),end = autrs.upper_bound(sch);
  78. beg != end; ++beg){
  79. cout << beg->second << endl;
  80. }
  81. cout << "-----------------" << endl;
  82. //方法3
  83. for(auto pos = autrs.equal_range(sch);pos.first != pos.second;++pos.first){
  84. cout << pos.first->second << endl;
  85. }
  86. }

github完整代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号