鱼C论坛

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

[学习笔记] 051-C++之STL-->map、multimap

[复制链接]
发表于 2018-9-21 20:29:45 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 moc 于 2018-9-21 20:29 编辑

1、map、multimap简介
     1. map是标准的关联式容器, 一个map是一个键值对序列,即(key,value)对,它提供基于key的快速检索能力,类似于python中的字典dict。
     2. map中的key值是唯一的。该容器中的元素是按一定的顺序排列,元素插入的过程是按照排序规则插入的,所以不能指定插入位置
     3. map的具体实现采用红黑树变体的平衡二叉树的数据结构。在插入和删除操作上比vector快。
     4. multimap与map的区别:map支持唯一键值,每个键只能出现一次,而multimap中相同的键可以出现多次,且multimap不支持[]操作
     5. #include <map>
2、map插入元素的四种方式
前三种方法  返回值为一个对组,即pair<iterator, bool>  _Pairib。
  1. map<int, string> map1;

  2. // 方法1
  3. map1.insert(pair<int, string>(1, "teacher01"));
  4. map1.insert(pair<int, string>(2, "teacher02"));

  5. // 方法2
  6. map1.insert(make_pair(3, "teacher03"));
  7. map1.insert(make_pair(4, "teacher04"));

  8. // 方法3
  9. map1.insert(map<int, string>::value_type(5, "teacher05"));
  10. pair<map<int, string>::iterator, bool> mypair1 = map1.insert(map<int, string>::value_type(6, "teacher06"));
  11. if (mypair1.second == true)
  12. {
  13.         cout << "插入成功!\n";
  14. }
  15. else
  16. {
  17.         cout << "插入失败!\n";
  18. }

  19. // 方法4
  20. map1[7] = "teacher07";
  21. map1[8] = "teacher08";

  22. // 容器的遍历
  23. for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
  24. {
  25.         cout << it->first << '\t' << it->second << endl;
  26. }

  27. for (auto each : map1)
  28. {
  29.         cout << each.first << '\t' << each.second << endl;
  30. }
  31. // map容器元素的删除
  32. while (!map1.empty())
  33. {
  34.         map<int, string>::iterator it = map1.begin();
  35.         map1.erase(it);
  36. }
复制代码

3、map基本操作
map的查找键 (含异常处理)
  1. map<int, string>::iterator it = map1.find(100);
  2. if (it == map1.end())
  3. {
  4.         cout << "key 100的值 不存在\n";
  5. }
  6. else
  7. {
  8.         cout << it->first << '\t' << it->second << endl;
  9. }
复制代码

方法:equal_range(n),返回两个迭代器 形成一个pair,pair中第一个为大于等于n的迭代器位置,第二个为大于n的迭代器位置。
  1. pair<map<int, string>::iterator, map<int, string>::iterator> mypair = map1.equal_range(5);
  2. if (mypair.first == map1.end())
  3. {
  4.         cout << "第一个迭代器  >= 5的位置  不存在\n";
  5. }
  6. else
  7. {
  8.         cout << mypair.first->first << '\t' << mypair.first->second << endl;

  9. }
  10. if (mypair.second == map1.end())
  11. {
  12.         cout << "第二个迭代器  >5的位置  不存在\n";
  13. }
  14. else
  15. {
  16.         cout << mypair.second->first << '\t' << mypair.second->second << endl;
  17. }
复制代码

4、multimap一键多值举例
  1. Staff s1("Tom1", 18), s2("Tom2", 23), s3("Jhon1", 24), s4("Jhon2", 18), s5("Alice", 20);
  2. multimap<string, Staff> map3;

  3. map3.insert(make_pair("Tom", s1));
  4. map3.insert(make_pair("Tom", s2));

  5. map3.insert(make_pair("Jhon", s3));
  6. map3.insert(make_pair("Jhon", s4));

  7. map3.insert(make_pair("Alice", s5));

  8. for (auto each : map3)
  9. {
  10.         cout << each.second.name << '\t' << each.second.age << endl;
  11. }

  12. int num = map3.count("Jhon");
  13. cout << "名为Jhon的个数:" << num << endl;
  14. multimap<string, Staff>::iterator it = map3.find("Jhon");

  15. int tag = 0;
  16. while (it != map3.end() && tag < num)
  17. {
  18.         cout << it->first << '\t' << it->second.name << endl;
  19.         it++;
  20.         tag++;
  21. }

  22. cout << "按照条件检索:\n";
  23. for (multimap<string, Staff>::iterator it = map3.begin(); it != map3.end(); it++)
  24. {
  25.         if (it->second.age == 20)
  26.         {
  27.                 cout << "年龄为20的是: " << it->second.name << endl;
  28.         }
  29. }
复制代码

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 21:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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