鱼C论坛

 找回密码
 立即注册
查看: 1179|回复: 0

[技术交流] C++旅程第18站——map

[复制链接]
发表于 2020-6-23 22:03:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
The trees that are slow to grow bear the best fruit.
                                                     Map
map中所有元素都是pair
pair中第一个元素都为key(键值),起到索引作用,第二元素为value(实值)
所有元素都会根据元素的键值自动排序

map/multimap属于关联式容器,底层结构是用二叉树实现。

优点:
可以根据key值快速找到value值

map和multimap区别:
map不允许容器中有重复key值元素
multimap允许容器中有重复key重复元素

  1. #include<iostream>
  2. #include<map>

  3. using namespace std;

  4. class Mapcompare
  5. {
  6.         public:
  7.                 bool operator()(int v1,int v2)
  8.                 {
  9.                         return v1 > v2;
  10.                 }
  11. };

  12. void printmap(map<int,int>&m)
  13. {
  14.         for (map<int,int>::iterator it = m.begin(); it != m.end(); it++)
  15.         {
  16.                 cout << "key = " << (*it).first << "  value = " << it->second << endl;
  17.         }
  18.         cout << endl;
  19. }

  20. void test1()
  21. {
  22.         map<int,int>m1;
  23.         
  24.         m1.insert(pair<int,int>(1,100));
  25.         m1.insert(pair<int,int>(2,200));
  26.         m1.insert(pair<int,int>(3,300));
  27.         m1.insert(pair<int,int>(4,400));
  28.         
  29.         printmap(m1);
  30.         
  31.         //拷贝构造
  32.         map<int,int>m2(m1);
  33.         printmap(m2);
  34.         
  35.         //赋值
  36.         map<int,int>m3;
  37.         m3 = m2;
  38.         printmap(m3);
  39.          
  40. }

  41. void test2()
  42. {
  43.         map<int,int>m1;
  44.         
  45.         m1.insert(pair<int,int>(1,200));
  46.         m1.insert(pair<int,int>(2,300));
  47.         m1.insert(pair<int,int>(3,400));
  48.         
  49.         if (m1.empty())
  50.         {
  51.                 cout << "m1为空!!!!!!" << endl;
  52.         }
  53.         else
  54.         {
  55.                 cout << "m1不为空!!!!!!!" << endl;
  56.                 cout << "m1的大小是: " << m1.size() << endl;
  57.         }
  58.         
  59.         map<int,int>m2;
  60.         
  61.         m2.insert(pair<int,int>(1,600));
  62.         m2.insert(pair<int,int>(2,700));
  63.         m2.insert(pair<int,int>(3,800));
  64.         
  65.         cout << "交换前!!!!!!!!!" << endl;
  66.         printmap(m1);
  67.         printmap(m2);
  68.         
  69.         cout << "交换后!!!!!!!!!!" << endl;
  70.         m1.swap(m2);
  71.         printmap(m1);
  72.         printmap(m2);
  73. }

  74. void test3()
  75. {
  76.         map<int,int> m1;
  77.         
  78.         //插入
  79.         m1.insert(pair<int,int>(1,10));
  80.         
  81.         m1.insert(make_pair(2,20));
  82.         
  83.         m1.insert(map<int,int>::value_type(3,30));
  84.         
  85.         m1[4] = 40;
  86.         
  87.         cout << m1[5] << endl;//系统会自动将value赋值为0
  88.         
  89.         printmap(m1);
  90.         
  91.         //删除
  92.         m1.erase(m1.begin());
  93.         printmap(m1);
  94.         
  95.         m1.erase(3);//可以按照key值进行删除
  96.         printmap(m1);
  97.         
  98.         //清空
  99.         m1.erase(m1.begin(),m1.end());
  100.         m1.clear();
  101.         printmap(m1);
  102. }

  103. void test4()
  104. {
  105.         map<int,int>m1;
  106.         
  107.         m1.insert(pair<int,int>(1,200));
  108.         m1.insert(pair<int,int>(2,300));
  109.         m1.insert(pair<int,int>(3,400));
  110.         
  111.         map<int,int>::iterator a = m1.find(2);
  112.         
  113.         if ( a != m1.end())
  114.         {
  115.                 cout << "查到元素 key =  " << (*a).first << " value : " << a->second << endl;
  116.         }
  117.         else
  118.         {
  119.                 cout << "未找到元素!!!!!!!!!!" << endl;
  120.         }
  121.         
  122.         //统计
  123.         //map不允许插入重复key元素,count统计结果不是 0 就是1
  124.         //multimap的count统计结果就可能大于1了
  125.         int num = m1.count(2);
  126.         cout << "num = " << num << endl;
  127. }

  128. void test5()
  129. {
  130.         map<int,int,Mapcompare>m1;
  131.         
  132.         m1.insert(pair<int,int>(1,200));
  133.         m1.insert(pair<int,int>(2,300));
  134.         m1.insert(pair<int,int>(3,400));
  135.         
  136.         for (map<int,int,Mapcompare>::iterator it = m1.begin(); it != m1.end(); it++)
  137.         {
  138.                 cout << "key = " << (*it).first << "  value = " << it->second << endl;
  139.         }
  140.         cout << endl;
  141. }

  142. int main()
  143. {
  144.         test1();//map构造和赋值
  145.         
  146.         test2();//map大小和交换
  147.         
  148.         test3();//map插入和删除
  149.         
  150.         test4();//map查找和统计
  151.         
  152.         test5();//map排序
  153.         
  154.         return 0;
  155. }
复制代码

                                                                              

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 19:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表