• C++ STL进阶与补充(map/multimap容器)


    Map中所有的容器都是pair。对组中每个中都有两个值,第一个值称为key值(键值),起到索引作用,第二个元素为value(实值)。

    所有元素会根据元素的键值自动排序。

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

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

    Map和multimap的区别:与set和multiset一样,主要区分key值有没有重复,value不做区分。

    (本节关于对组容器的介绍请参考本人其他博客:《C++ STL进阶与补充(set/multiset容器)》)

    1、map构造和赋值

    Map<T1, T2> mp;

    默认构造

    Map(const map& mp);

    拷贝构造

    =

    等号赋值

    1. #include<iostream>
    2. #include<map>
    3. using namespace std;
    4. void printMap(const map<int,int>& m)
    5. {
    6. for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
    7. {
    8. cout << "Key: " << (*it).first << " Value: " << (*it).second << endl;
    9. }
    10. }
    11. void test01()
    12. {
    13. map<int, int> m1; //默认构造
    14. m1.insert(pair<int, int>(1, 10)); //每次都插入匿名对组
    15. m1.insert(pair<int, int>(2, 20));
    16. m1.insert(pair<int, int>(5, 50));
    17. m1.insert(pair<int, int>(3, 30));
    18. m1.insert(pair<int, int>(4, 40));
    19. printMap(m1); //会自动排序
    20. //拷贝构造
    21. map<int, int> m2(m1);
    22. printMap(m2);
    23. //赋值
    24. map<int, int> m3;
    25. m3 = m2;
    26. printMap(m3);
    27. }
    28. int main()
    29. {
    30. test01();
    31. }

    2、map大小和交换

    Size()

    返回容器中元素个数

    Empty()

    判断容器是否为空

    Swap(st)

    交换两个集合容器

    1. #include<iostream>
    2. #include<map>
    3. using namespace std;
    4. void printMap(const map<int,int>& m)
    5. {
    6. for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
    7. {
    8. cout << "Key: " << (*it).first << " Value: " << (*it).second << endl;
    9. }
    10. }
    11. void test01()
    12. {
    13. map<int, int> m1;
    14. m1.insert(pair<int, int>(1, 10));
    15. m1.insert(pair<int, int>(2, 20));
    16. m1.insert(pair<int, int>(5, 50));
    17. if (m1.empty())
    18. {
    19. cout << "m为空!" << endl;
    20. }
    21. else
    22. {
    23. cout << "m不为空,m的大小为:" << m1.size() << endl;
    24. }
    25. printMap(m1); //会自动排序
    26. }
    27. void test02()
    28. {
    29. map<int, int> m1;
    30. m1.insert(pair<int, int>(1, 10));
    31. m1.insert(pair<int, int>(2, 20));
    32. m1.insert(pair<int, int>(3, 30));
    33. map<int, int> m2;
    34. m2.insert(pair<int, int>(4, 100));
    35. m2.insert(pair<int, int>(5, 200));
    36. m2.insert(pair<int, int>(6, 300));
    37. cout << "交换前:" << endl;
    38. printMap(m1);
    39. printMap(m2);
    40. cout << "交换后:" << endl;
    41. m2.swap(m1);
    42. printMap(m1);
    43. printMap(m2);
    44. }
    45. int main()
    46. {
    47. //test01();
    48. test02();
    49. }

    3、map插入和删除

    Insert(elem)

    插入元素,每个elem都是对组

    Clear()

    清空

    Erase(pos)

    删除pos迭代器位置的元素,返回下一个元素的迭代器

    Erase(beg,end)

    删除区间[beg,end]的所有元素,返回下一个元素的迭代器

    Erase(key)

    删除容器中键值为key的数据

    1. #include<iostream>
    2. #include<map>
    3. using namespace std;
    4. void printMap(const map<int,int>& m)
    5. {
    6. for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
    7. {
    8. cout << "Key: " << (*it).first << " Value: " << (*it).second << endl;
    9. }
    10. }
    11. void test01()
    12. {
    13. map<int, int> m1;
    14. //第一种插入
    15. m1.insert(pair<int, int>(1, 10));
    16. //第二种插入
    17. m1.insert(make_pair(2,20));
    18. //第三种插入
    19. m1.insert(map<int, int>::value_type(3, 30));
    20. //第四种插入:中括号方式,但是不建议
    21. //中括号的用途主要用于根据键值访问实值
    22. m1[4] = 40;
    23. cout << m1[5] << endl; //虽然没有5,但是会自动创建5
    24. printMap(m1);
    25. //按照迭代器位置删除
    26. m1.erase(m1.begin());
    27. printMap(m1);
    28. //按照key删除
    29. m1.erase(5);
    30. printMap(m1);
    31. }
    32. int main()
    33. {
    34. test01();
    35. }

    4、map查找和统计

    对map容器进行查找数据及统计数据。

    Find(key)

    查找key是否存在,若存在则返回键的元素的迭代器;若不存在,返回map.end()

    Count(key)

    统计key的元素个数,对于map容器,只有0和1两种结果。

    1. #include<iostream>
    2. #include<map>
    3. using namespace std;
    4. void printMap(const map<int,int>& m)
    5. {
    6. for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
    7. {
    8. cout << "Key: " << (*it).first << " Value: " << (*it).second << endl;
    9. }
    10. }
    11. void test01()
    12. {
    13. map<int, int> m1;
    14. m1.insert(pair<int, int>(1, 10));
    15. m1.insert(pair<int, int>(2, 20));
    16. m1.insert(pair<int, int>(3, 30));
    17. map<int, int>::iterator pos = m1.find(3);
    18. if (pos != m1.end())
    19. {
    20. cout << "找到元素key=" << (*pos).first<< endl;
    21. cout << "找到元素value=" << (*pos).second<< endl;
    22. }
    23. else
    24. {
    25. cout << "未找到该元素!" << endl;
    26. }
    27. //统计
    28. int num = m1.count(3);
    29. cout << "num=" << num << endl;
    30. }
    31. int main()
    32. {
    33. test01();
    34. }

    5、map排序

    Map容器默认的排序规则为按照key值从小到大排序,可利用仿函数改变排序规则。

    对于自定义数据类型,必须要指定排序规则。

    1. #include<iostream>
    2. #include<map>
    3. using namespace std;
    4. class myCompare
    5. {
    6. public:
    7. bool operator()(int v1, int v2) const
    8. {
    9. return v1 > v2;
    10. }
    11. };
    12. void printMap(const map<int,int,myCompare>& m)
    13. {
    14. for (map<int, int,myCompare>::const_iterator it = m.begin(); it != m.end(); it++)
    15. {
    16. cout << "Key: " << (*it).first << " Value: " << (*it).second << endl;
    17. }
    18. }
    19. void test01()
    20. {
    21. map<int, int, myCompare> m1;
    22. m1.insert(pair<int, int>(1, 10));
    23. m1.insert(pair<int, int>(2, 20));
    24. m1.insert(pair<int, int>(3, 30));
    25. m1.insert(pair<int, int>(4, 40));
    26. printMap(m1);
    27. }
    28. int main()
    29. {
    30. test01();
    31. }
  • 相关阅读:
    8.MySQL DCL (数据控制语言) 语句
    【yolov8部署实战】VS2019环境下使用C++和OpenCV环境部署yolo项目|含详细注释源码
    神经元人体分布大图,人体的神经元图片
    Java中如何删除HashSet对象中的元素呢?
    【LeetCode热题100】--15.三数之和
    在reactNative中使用mobx
    第22章_瑞萨MCU零基础入门系列教程之DMA控制器
    【Linux】常用工具(上)
    lxml基本使用
    el-collapse 嵌套中 el-checkbox作为标题,选中复选框与el-tree联动
  • 原文地址:https://blog.csdn.net/Lao_tan/article/details/125468665