技术在于交流、沟通,本文为博主原创文章转载请注明出处并保持作品的完整性

map与multimap为关联容器,结构如下

map底层实现依然是rb_tree 他的data可以改,但是key不能改,因此map仍然具有自动排序的功能

我们无法使用迭代器改变元素的key(const key),但是可以改变元素的data.

map的key必须独一无二,multimap的key可以重复

map的定义函数

  1. template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
  2. typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  3. class map
  4. {
  5. public:
  6. typedef _Key key_type;
  7. typedef _Tp mapped_type;
  8. typedef std::pair<const _Key, _Tp> value_type;
  9. typedef _Compare key_compare;
  10. typedef _Alloc allocator_type;
  11. ...
  12. }

参数1 class key 键值key

参数2 class T data

参数3 class compare 排序key的函数 默认为less() 升序

参数4 alloc 分配器


map的基本使用

一 定义

  1. //构造函数
  2. map<int, int> c;
  3. c[] = ;
  4. c[] = ;
  5. c[] = ;
  6. c[] = ;
  7. c[] = ;
  8. for(auto i : c)
  9. {
  10. cout<<"["<< i.first <<"] = " << i.second <<" ";
  11. }
  12. cout << endl;
  13.  
  14. //operator =
  15. map<int, int> c1;
  16. c1 = c;
  17. for(auto i : c)
  18. {
  19. cout<<"["<< i.first <<"] = " << i.second <<" ";
  20. }
  21. cout << endl;

二 迭代器操作 map的迭代器就是红黑树的迭代器

  1. //迭代器操作
  2. /*
  3. map<int, int> c;
  4. c.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
  5. */
  6.  
  7. //begin()
  8. map<int, int>::iterator iter;
  9. iter = c.begin();
  10. cout<< "begin(): "<<"["<< iter->first <<"] = " << iter->second <<endl;
  11.  
  12. //end()
  13. iter = c.end();
  14. iter--;
  15. cout<<"end(): " <<"["<< iter->first <<"] = " << iter->second <<endl;
  16.  
  17. //rbegin()反向头迭代器
  18. map<int, int>::reverse_iterator riter;
  19. riter = c.rbegin();
  20. cout << "rbegin(): "<<"["<< riter->first <<"] = " << riter->second <<endl;
  21.  
  22. //rend()反向头迭代器
  23. riter = c.rend();
  24. riter--;
  25. cout << "rend(): "<<"["<< riter->first <<"] = " << riter->second <<endl;
  26.  
  27. //cbegin() const 迭代器 正向 头迭代器
  28. map<int, int>::const_iterator citer;
  29. citer = c.cbegin();
  30. cout << "cbegin(): "<<"["<< citer->first <<"] = " << citer->second <<endl;
  31.  
  32. //cend() const 迭代器 反向 尾迭代器
  33. citer = c.cend();
  34. citer--;
  35. cout<< "cend(): "<<"["<< citer->first <<"] = " << citer->second <<endl;

三 容量

  1. //容量
  2. /*
  3. map<int, int> c;
  4. c.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
  5. */
  6. //是否为kong
  7. cout << "empty: " << c.empty() <<endl;
  8.  
  9. //元素个数
  10. cout << "size: " << c.size() <<endl;
  11.  
  12. //最大容量
  13. cout << "max_size: " << c.max_size() <<endl;

四 基本操作

  1. //基本操作
  2. /*
  3. map<int, int> c;
  4. c.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
  5. */
  6.  
  7. //operator[] 这个和其他容器的operator有些不同,如果[index] 的index(key) 在map中存在则直接返回该key对应的data ,如果不存在则想该位置插入默认值
  8. cout << "operator[]: " << c[] << endl;
  9. cout << "operator[]: " << c[] << endl; //这时你会发现 map自动插入一个c[10]
  10. for(auto i : c)
  11. {
  12. cout<<"["<< i.first <<"] = " << i.second <<" ";
  13. }
  14. cout << endl;
  15.  
  16. //at() 取data
  17. cout<< "at(): " << c.at() << endl;
  18.  
  19. //插入insert() map 不允许key重复 插入重复key 不会报错但插入不成功
  20. c.insert(pair<int, int>(, ));
  21. cout <<"insetr(): ";
  22. for(auto i : c)
  23. {
  24. cout<<"["<< i.first <<"] = " << i.second <<" ";
  25. }
  26. cout << endl;
  27.  
  28. map<int, int> c_insert;
  29. map<int, int>::iterator insert_iter = c_insert.begin();
  30. c_insert.insert(insert_iter,pair<int, int>(, ));
  31. cout <<"insetr(): ";
  32. for(auto i : c_insert)
  33. {
  34. cout<<"["<< i.first <<"] = " << i.second <<" ";
  35. }
  36. cout << endl;
  37.  
  38. //删除
  39. map<int,int>::iterator erase_iter = c.begin();
  40. c.erase(erase_iter);
  41. cout <<"erase(): ";
  42. for(auto i : c)
  43. {
  44. cout<<"["<< i.first <<"] = " << i.second <<" ";
  45. }
  46. cout << endl;
  47. //指定下表删除
  48. c.erase();
  49. cout <<"erase(): ";
  50. for(auto i : c)
  51. {
  52. cout<<"["<< i.first <<"] = " << i.second <<" ";
  53. }
  54. cout << endl;
  55.  
  56. //指定元素删除
  57. erase_iter = c.find();
  58. if(erase_iter != c.end())
  59. {
  60. cout<<"found index: "<< erase_iter->first <<endl;
  61. c.erase(erase_iter);
  62. }
  63. else{
  64. cout<< "Not found" <<endl;
  65. }
  66. for(auto i : c)
  67. {
  68. cout<<"["<< i.first <<"] = " << i.second <<" ";
  69. }
  70. cout << endl;
  71.  
  72. //交换swap
  73. map<int, int> c_swap1;
  74. c_swap1.insert({pair<int, int>(,),pair<int, int>(,),pair<int, int>(,),pair<int, int>(,),pair<int, int>(,),pair<int, int>(,)});
  75. map<int, int> c_swap2;
  76. cout<<"swap() before: "<<endl;
  77. cout<<"c_swap1: ";
  78. for(auto i : c_swap1)
  79. {
  80. cout<<"["<< i.first <<"] = " << i.second <<" ";
  81. }
  82. cout << endl;
  83.  
  84. cout<<"c_swap2: ";
  85. for(auto i : c_swap2)
  86. {
  87. cout<<"["<< i.first <<"] = " << i.second <<" ";
  88. }
  89. cout << endl;
  90.  
  91. cout<<"swap() after: ";
  92. c_swap2.swap(c_swap1);
  93. cout<<"c_swap1: ";
  94. for(auto i : c_swap1)
  95. {
  96. cout<<"["<< i.first <<"] = " << i.second <<" ";
  97. }
  98. cout << endl;
  99.  
  100. cout<<"c_swap2: ";
  101. for(auto i : c_swap2)
  102. {
  103. cout<<"["<< i.first <<"] = " << i.second <<" ";
  104. }
  105. cout << endl;
  106.  
  107. //清除clear
  108. c_insert.clear();
  109. cout <<"clear(): ";
  110. for(auto i : c_insert)
  111. {
  112. cout<<"["<< i.first <<"] = " << i.second <<" ";
  113. }
  114. cout << endl;
  115.  
  116. //elements() 插入 如果存在什么也不做 如果不存在插入
  117. map<int, int>::iterator element_iter = c_swap2.begin();
  118.  
  119. auto xxx = c_swap2.emplace(pair<int, int>(,));
  120. if(xxx.second)
  121. {
  122. cout << "不存在" <<endl;
  123. }
  124. else
  125. {
  126. cout<< "存在" <<endl;
  127. }
  128. cout <<"elements(): ";
  129. for(auto i : c_swap2)
  130. {
  131. cout<<"["<< i.first <<"] = " << i.second <<" ";
  132. }
  133. cout << endl;
  134.  
  135. //emplace_hint() 插入
  136. element_iter = c.emplace_hint(element_iter, pair<int, int>(,));
  137. cout <<"emplace_hint(): ";
  138. if(element_iter != c_swap2.end())
  139. {
  140. cout<< "存在" <<endl;
  141. }
  142. else{
  143. cout << "不存在" <<endl;
  144. }

五 操作函数

  1. //函数
  2. /*
  3. map<int, int> c;
  4. c.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
  5. */
  6. //key_comp 返回key排序的函数 返回仿函数
  7. cout<<"key_comp(): " << c.key_comp()(,) <<endl; //会返回1 因为1<2
  8. cout<<"key_comp(): " << c.key_comp()(,) <<endl; //会返回0 因为2>1
  9. cout<<"key_comp(): " << c.key_comp()(,) <<endl; //会返回0 因为1=1
  10.  
  11. //value_comp 返回取value和key数据包中的 取key函数 返回仿函数
  12. pair<int,int> value_comp_pair = *c.begin();
  13. iter = c.begin();
  14. cout << c.value_comp()(*iter++,value_comp_pair) << endl;

六 算法

  1. //算法
  2. /*
  3. map<int, int> c;
  4. c.insert({pair<int, int>(1,10),pair<int, int>(2,20),pair<int, int>(3,30),pair<int, int>(4,40),pair<int, int>(5,50),pair<int, int>(6,60)});
  5. */
  6. //find() 指定key 返回对应pair
  7. cout <<"find(1): " << c.find()->second << endl;;
  8.  
  9. //count() key出现的次数
  10. cout <<"count(1): " << c.count()<< endl;;
  11.  
  12. c.erase();
  13.  
  14. //lower_bound 返回键值>=给定元素的第一个位置
  15. auto lower_boundObj = c.lower_bound();
  16. if(lower_boundObj->first)
  17. cout<<lower_boundObj->first<<endl;
  18. else
  19. cout<< "Not found lower_boundObj" << endl;
  20.  
  21. for(auto i : c)
  22. {
  23. cout<<"["<< i.first <<"] = " << i.second <<" ";
  24. }
  25. cout << endl;
  26.  
  27. //upper_bound 返回键值>给定元素的第一个位置
  28. auto upper_boundObj = c.upper_bound();
  29. if(upper_boundObj->first)
  30. cout<<upper_boundObj->first<<endl;
  31. else
  32. cout<< "Not found upper_bound" << endl;
  33.  
  34. for(auto i : c)
  35. {
  36. cout<<"["<< i.first <<"] = " << i.second <<" ";
  37. }
  38. cout << endl;
  39.  
  40. //equal_range()返回该元素所在区间(闭区间),返回值是一个pair<iterator, iterator>类型,first代表所在区间的起点迭代器,second表示所在区间的终点迭代器
  41.  
  42. auto equal_rangeObj = c.equal_range();
  43. if(equal_rangeObj.first->first)
  44. {
  45. cout<<equal_rangeObj.first->first<<endl;
  46. }
  47. else
  48. cout<< "NOT equal_rangeObj.first" << endl;
  49.  
  50. if(equal_rangeObj.second->second)
  51. {
  52. cout<<equal_rangeObj.second->first<<endl;
  53. }
  54. else
  55. cout<< "NOT second" << endl;

七 自定义比较函数 map 默认为升序 改为降序

  1. class my_compare_
  2. {
  3. public:
  4. bool operator()(int a, int b)
  5. {
  6. return a > b;
  7. }
  8. };
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. map<int, int, my_compare_> c;
  13. c.insert({pair<int, int>(,),pair<int, int>(,),pair<int, int>(,),pair<int, int>(,),pair<int, int>(,),pair<int, int>(,)});
  14. for(auto i : c)
  15. {
  16. cout<<"["<< i.first <<"] = " << i.second <<" ";
  17. }
  18. cout << endl;
  19. return ;
  20. }

STL标准库-容器-map和multimap的更多相关文章

  1. STL标准库-容器-set与map

    STL标准库-容器-set与multiset C++的set https://www.cnblogs.com/LearningTheLoad/p/7456024.html STL标准库-容器-map和 ...

  2. STL标准库-容器-set与multiset

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. set与multiset关联容器 结构如下 set是一种关联容器,key即value,value即key.它是自动排序,排序特点依据key se ...

  3. STL标准库-容器-deque

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性. deque双向开口可进可出的容器 我们知道连续内存的容器不能随意扩充,因为这样容易扩充别人那去 deque却可以,它创造了内存 ...

  4. STL标准库-容器-vector

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性. 向量容器vector是一个动态数组,内存连续,它是动态分配内存,且每次扩张的原来的二倍. 他的结构如下 一 定义 vector ...

  5. STL学习笔记— —容器map和multimap

    简单介绍 在头文件<map> 中定义 namespace std { template <typename Key, typename T, typename Compare = l ...

  6. STL标准库-容器-list

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性. list 表示非连续的内存区域,并通过一对指向首尾元素的指针双向链接起来,从而允许向前和向后两个方向进行遍历.在list 的任 ...

  7. STL标准库-容器-rb_tree

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 红黑树,关联式容器底层实现(map set),在使用中基本运用不到,但是还是想了解一下他的运作方式 Red_Black tree ...

  8. 关于C++ STL标准库中map 的多元素应用

    map的特性是,所有的元素会根据键值自动排序.map的所有元素都是pair,同时拥有实值(value)和键值(key).pair的第一个元素被视为键值,第二个被视为实质piar 的定义 templat ...

  9. STL标准库-容器适配器

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 上一节介绍了仿函数适配器,这节主要介绍容器适配器和迭代器适配器的概念,其实容器适配器和迭代器其适配器就是封装了一些其他class ...

随机推荐

  1. python 面向对象 类 __doc__

    用来打印类的描述信息 class A1(object): '''类的描述信息''' print(A1.__doc__) # 类的描述信息

  2. python 定义类 学习2

    构造函数的变量 也叫做 实例变量 class role(): # 传参数 def __init__(self,name,role,weapon,life_value=100,moneny=15000) ...

  3. leetcode——Search for a Range 排序数组中寻找目标下标范围(AC)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  4. (转)二十三种设计模式及其python实现

    本文源码寄方于github:https://github.com/w392807287/Design_pattern_of_python 参考文献: <大话设计模式>——吴强 <Py ...

  5. t检验&z检验学习[转载]

    转自:https://blog.csdn.net/m0_37777649/article/details/74937242 1.什么是T检验? T检验是假设检验的一种,又叫student t检验(St ...

  6. AngularJS SQL

    服务端代码 以下列出了列出了几种服务端代码类型: 使用 PHP 和 MySQL.返回 JSON. 使用 PHP 和 MS Access.返回 JSON. 使用 ASP.NET, VB, 及 MS Ac ...

  7. HTML5游戏开发系列教程10(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-10/ 最后我们将继续使用canvas来进行HTML5游戏开发系列 ...

  8. 推荐一个js脚本的字体拟合模型

    推荐一个js脚本的字体拟合模型 http://r3mi.github.io/poly2tri.js/   推荐一个js脚本的字体拟合模型 http://r3mi.github.io/poly2tri. ...

  9. iview使用vue-i18n实现国际化

    iview官网中和网上的例子中使用的都是webpack方式,需要import js文件,但是由于项目架构比较简单,没有使用webpack,纯html和js进行交互.所以这里就直接使用js文件引用方式. ...

  10. Linux内核分析08

    进程的切换和系统的一般执行过程 一,进程切换的关键代码switch_to分析 进程调度的时机 中断处理过程(包括时钟中断.I/O中断.系统调用和异常)中,直接调用schedule(),或者返回用户态时 ...