• C++学习 --map


    目录

    1, 什么是map

    2, 创建map

    2-1, 标准数据类型

    2-2, 自定义数据类型

    2-3, 其他创建方式

    3, 操作map

    3-1, 赋值

    3-2, 插入元素(insert)

    3-2-1, 插入标准数据类型

    3-2-1, 插入自定义数据类型

    3-3, 查询元素

    3-3-1, 标准数据类型查询

    3-3-1-1,迭代器访问 

    3-3-1-2, 键访问

    3-3-2, 自定义数据类型查询

    3-3-2-1, 迭代器访问

    3-3-2-2, 键访问

    3-4, 移除元素

    3-4-1, 通过键移除

    3-4-2, 通过迭代器移除

    3-5, 清空

    3-6, 获取长度

    3-7, 查找元素

    3-8, 判断map是否为空

    3-9, 统计键数量

    3-10, map互换

    4, multimap


    1, 什么是map

    map中存储的元素是pair, 元素会根据pair的key自动升序排序, 通过key快速找到对应的value

    分为mapmultimap两种, 前者不允许key重复, 后者允许key重复

    由于不是标准数据类型, 需要包含头文件#include 才能使用

    2, 创建map

    通过map<数据类型, 数据类型> 对象名, 可创建map, 数据类型可以是标准数据类型, 也可以是自定义类型

    2-1, 标准数据类型

    1. //map中元素键数据类型为string, 值数据类型为int
    2. mapint> m1;

    2-2, 自定义数据类型

    1. Myclass1 mc_1("张三", 10);
    2. Myclass2 mc_2("李四", 20);
    3. //map中元素键数据类型为MyClass, 值数据类型为MyClass2
    4. map m1;

    2-3, 其他创建方式

    1. //通过拷贝构造方式, 将一个已存在的m1, 拷贝给m2
    2. mapint> m2(m1);
    3. //通过迭代器方式, 将一个已存在的m1, 拷贝给m2
    4. //m1.begin()指向m1的第一个元素, m1.begin()指向m1的最后一个元素的下一个元素
    5. mapint> m3(m1.begin(), m1.end());

    3, 操作map

    3-1, 赋值

    由于map是一个容器, 只支持将一个map赋值给另外一个map

    1. mapint> m1;
    2. mapint> m2;
    3. m2 = m1;

    3-2, 插入元素(insert)

    通过对象名.insert(pair对象), 向map中插入元素, 不支持虚插(push_back)

    3-2-1, 插入标准数据类型

    1. mapint> m1;
    2. //下面三种都可以插入数据
    3. //注意:只能插入pair对象
    4. m1.insert(pairint>("aaa", 1));
    5. m1.insert(make_pair("bbb", 2));
    6. m1.insert(mapint>::value_type("ccc", 3));

    3-2-1, 插入自定义数据类型

    1. //重定义仿函数, 注意后面加了const,是个常函数
    2. class MyCompare
    3. {
    4. public:
    5. bool operator()(const Myclass1& m1, const Myclass1& m2) const
    6. {
    7. return m1.m_name > m2.m_name;
    8. }
    9. };
    10. Myclass1 mc_1("aaa", 1);
    11. Myclass2 mc_2("bbb", 2);
    12. //由于key值是自定义数据类型,这里需要重定义仿函数, 数据加上MyCompare
    13. map m1;
    14. //下面三种都可以插入数据
    15. //注意:只能插入pair对象
    16. m1.insert(pair(mc_1, mc_2));
    17. m1.insert(make_pair(mc_1, mc_2));
    18. m1.insert(map::value_type(mc_1, mc_2));

    3-3, 查询元素

    可通过迭代器或者对象名[key]的方式

    3-3-1, 标准数据类型查询

    3-3-1-1,迭代器访问 
    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. void print_map(mapint>& m)
    6. {
    7. for (mapint>::iterator it = m.begin(); it != m.end(); it++)
    8. {
    9. cout << "键:" << it->first << " " << "值:" << it->second << endl;
    10. }
    11. }
    12. int main()
    13. {
    14. mapint> m1;
    15. //下面三种都可以插入数据
    16. //注意:只能插入pair对象
    17. m1.insert(pairint>("aaa", 111));
    18. m1.insert(make_pair("bbb", 222));
    19. m1.insert(mapint>::value_type("ccc", 333));
    20. print_map(m1);
    21. system("pause");
    22. return 0;
    23. }
    3-3-1-2, 键访问
    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. mapint> m1;
    8. //下面三种都可以插入数据
    9. //注意:只能插入pair对象
    10. m1.insert(pairint>("aaa", 111));
    11. m1.insert(make_pair("bbb", 222));
    12. m1.insert(mapint>::value_type("ccc", 333));
    13. cout << "通过键获取元素:" << m1["aaa"] << endl;
    14. system("pause");
    15. return 0;
    16. }

    3-3-2, 自定义数据类型查询

    3-3-2-1, 迭代器访问
    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. class Myclass1
    6. {
    7. public:
    8. Myclass1(string name, int age) : m_name(name), m_age(age) {};
    9. string m_name;
    10. int m_age;
    11. };
    12. class Myclass2
    13. {
    14. public:
    15. Myclass2(string name, int age) : m_name(name), m_age(age) {};
    16. string m_name;
    17. int m_age;
    18. };
    19. //仿函数重载
    20. class MyCompare
    21. {
    22. public:
    23. bool operator()(const Myclass1& m1, const Myclass1& m2) const
    24. {
    25. return m1.m_name < m2.m_name;
    26. }
    27. };
    28. //注意map中加上了MyCompare
    29. void print_map_1(map& m)
    30. {
    31. for (map::iterator it = m.begin(); it != m.end(); it++)
    32. {
    33. cout << "键:" << it->first.m_name << " " << it->first.m_age << endl;
    34. cout << "值:" << it->second.m_name << " " << it->first.m_age << endl;
    35. }
    36. }
    37. int main()
    38. {
    39. Myclass1 mc_1("aaa", 1);
    40. Myclass2 mc_2("bbb", 2);
    41. Myclass1 mc_3("ccc", 3);
    42. Myclass2 mc_4("ddd", 4);
    43. Myclass1 mc_5("eee", 5);
    44. Myclass2 mc_6("fff", 6);
    45. //由于key值是自定义数据类型,这里需要重定义仿函数, 数据加上MyCompare
    46. map m1;
    47. //下面三种都可以插入数据
    48. //注意:只能插入pair对象
    49. m1.insert(pair(mc_1, mc_2));
    50. m1.insert(make_pair(mc_3, mc_4));
    51. m1.insert(map::value_type(mc_5, mc_6));
    52. print_map_1(m1);
    53. system("pause");
    54. return 0;
    55. }
    3-3-2-2, 键访问

    在已知键的情况下, 通过对象名[键], 获取元素

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. class Myclass1
    6. {
    7. public:
    8. //通过键获取元素,需定义默认构造函数:Myclass1() {}
    9. Myclass1() {};
    10. Myclass1(string name, int age) : m_name(name), m_age(age) {};
    11. string m_name;
    12. int m_age;
    13. };
    14. class Myclass2
    15. {
    16. public:
    17. //通过键获取元素,需定义默认构造函数:Myclass1() {}
    18. Myclass2() {};
    19. Myclass2(string name, int age) : m_name(name), m_age(age) {};
    20. string m_name;
    21. int m_age;
    22. };
    23. //仿函数重载
    24. class MyCompare
    25. {
    26. public:
    27. bool operator()(const Myclass1& m1, const Myclass1& m2) const
    28. {
    29. return m1.m_name < m2.m_name;
    30. }
    31. };
    32. int main()
    33. {
    34. Myclass1 mc_1("aaa", 1);
    35. Myclass2 mc_2("bbb", 2);
    36. Myclass1 mc_3("ccc", 3);
    37. Myclass2 mc_4("ddd", 4);
    38. Myclass1 mc_5("eee", 5);
    39. Myclass2 mc_6("fff", 6);
    40. //由于key值是自定义数据类型,这里需要重定义仿函数, 数据加上MyCompare
    41. map m1;
    42. //下面三种都可以插入数据
    43. //注意:只能插入pair对象
    44. m1.insert(pair(mc_1, mc_2));
    45. m1.insert(make_pair(mc_3, mc_4));
    46. m1.insert(map::value_type(mc_5, mc_6));
    47. //通过键访问
    48. cout << "通过键获取元素:" << m1[mc_3].m_name << " " << m1[mc_3].m_age << endl;
    49. system("pause");
    50. return 0;
    51. }

    3-4, 移除元素(erase)

    3-4-1, 通过键移除

    通过对象名.erase(键), 指定从map中移除元素

    m1.erase("bbb");

    3-4-2, 通过迭代器移除

    通过对象名.erase(迭代器), 指定从map中移除元素

    1. mapint>::iterator it = m1.begin();
    2. //移动迭代器的位置
    3. it = it++;
    4. m1.erase(it);

    3-5, 清空(clear)

    通过对象名.clear(), 清空map中的元素

    1. m1.clear();
    2. //通过指定迭代器区间进行清空
    3. m1.erase(m1.begin(), m1.end());

    3-6, 获取长度(size)

    通过对象名.size(), 获取map的元素个数

    m1.size();

    3-7, 查找元素(find)

    通过对象名.find(键), 查找map中是否在存在元素, 存在返回对应iterator, 不存在返回对象名.end()

    1. //存在返回对应的iterator
    2. mapint>::iterator ret = m1.find("aaa");
    3. if (ret != m1.end())
    4. {
    5. cout << "键:" << ret->first << " 值:" << ret->second << endl;
    6. }
    7. else
    8. {
    9. cout << "查找的键不存在" << endl;
    10. }

    3-8, 判断map是否为空(empty)

    通过对象名.empty(), 判断map是否为空, 为空返回1, 不为空返回0;

    m1.empty();

    3-9, 统计键数量(count)

    通过对象名.count(键), 统计map中指定的键的个数,要么为1,要么为0

    m1.count("aaa");

    3-10, map互换(swap)

    通过对象名1.swap(对象名2), 可交换两个map的元素

    m1.swap(m2);

    4, multimap

    通过multimap<数据类型, 数据类型> 对象名, 创建multimap, multimap支持键重复

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. void print_map(multimapint>& m)
    6. {
    7. for (multimapint>::iterator it = m.begin(); it != m.end(); it++)
    8. {
    9. cout << "键:" << it->first << " " << "值:" << it->second << endl;
    10. }
    11. }
    12. int main()
    13. {
    14. multimapint> mul_m1;
    15. mul_m1.insert(make_pair("aaa", 111));
    16. mul_m1.insert(make_pair("aaa", 112));
    17. mul_m1.insert(make_pair("aaa", 113));
    18. print_map(mul_m1);
    19. system("pause");
    20. return 0;
    21. }

  • 相关阅读:
    【5GC】5G PDU会话以及会话类型
    STM32WB55的FUS更新及协议栈固件烧写方法
    主流MQ对比和选型
    ASP.NET CORE使用WebUploader对大文件分片上传,实时通知前端上传进度
    算法题:203. 移除链表元素(递归法、设置虚拟头节点法等3种方法)Java实现创建链表与解析链表
    [vue] vue2 Invalid Host header 解决问题
    【Linux】Linux常用操作命令(三)
    docker打包container成image,然后将image上传到docker hub
    Git工作原理
    Redis持久化(RDB AOF)
  • 原文地址:https://blog.csdn.net/qq_25500415/article/details/134484888