• 81.C++ STL map/ multimap容器


    目录

    1.map基本概念

    2.构造和赋值函数 

    3.map的大小和交换

    4.map的插入和删除

    5.map的查找和统计

    6.map容器排序


    1.map基本概念

    简介:

    • map中所有元素都是pair
    • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
    • 所有元素都会根据元素的键值自动排序

    本质:

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

    优点:

    • 可以根据key值快速找到value值

    map和multimap区别

    • map不允许容器中有重复key值元素
    • multimap允许容器中有重复key值元素

    2.构造和赋值函数 

    1. map mapTT;//map默认构造函数:
    2. map(const map &mp);//拷⻉构造函数
    3. map& operator=(const map &mp);//重载等号操作符
    1. #include
    2. #include
    3. using namespace std;
    4. void printMap(map<int, int>&m)
    5. {
    6. for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
    7. {
    8. cout << "key = " << it->first << " value = " << it->second << endl;
    9. }
    10. cout << endl;
    11. }
    12. void test01()
    13. {
    14. map<int, int>m; //默认构造
    15. m.insert(pair<int, int>(1, 10));
    16. m.insert(pair<int, int>(2, 20));
    17. m.insert(pair<int, int>(3, 30));
    18. printMap(m);
    19. map<int, int>m2(m); //拷贝构造
    20. printMap(m2);
    21. map<int, int>m3;
    22. m3 = m2; //赋值
    23. printMap(m3);
    24. }
    25. int main() {
    26. test01();
    27. system("pause");
    28. return 0;
    29. }

    3.map的大小和交换

    1. size();//返回容器中元素的数⽬
    2. empty();//判断容器是否为空
    3. swap(mp);//交换两个集合容器
    1. void printMap(map<int, int>& m)
    2. {
    3. for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
    4. {
    5. cout << "key = " << it->first << " value = " << it->second << endl;
    6. }
    7. cout << endl;
    8. }
    9. void test01()
    10. {
    11. map<int, int>m;
    12. m.insert(pair<int, int>(1, 10));
    13. m.insert(pair<int, int>(2, 20));
    14. m.insert(pair<int, int>(3, 30));
    15. if (m.empty())
    16. {
    17. cout << "m为空" << endl;
    18. }
    19. else
    20. {
    21. cout << "m不为空" << endl;
    22. cout << "m的大小为: " << m.size() << endl;
    23. }
    24. }
    25. //交换
    26. void test02()
    27. {
    28. map<int, int>m;
    29. m.insert(pair<int, int>(1, 10));
    30. m.insert(pair<int, int>(2, 20));
    31. m.insert(pair<int, int>(3, 30));
    32. map<int, int>m2;
    33. m2.insert(pair<int, int>(4, 100));
    34. m2.insert(pair<int, int>(5, 200));
    35. m2.insert(pair<int, int>(6, 300));
    36. cout << "交换前" << endl;
    37. printMap(m);
    38. printMap(m2);
    39. cout << "交换后" << endl;
    40. m.swap(m2);
    41. printMap(m);
    42. printMap(m2);
    43. }

    4.map的插入和删除

    1. insert(elem); //在容器中插入元素。
    2. clear(); //清除所有元素
    3. erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
    4. erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
    5. erase(key); //删除容器中值为key的元素。
    1. void printMap(map<int, int>& m)
    2. {
    3. for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
    4. {
    5. cout << "key = " << it->first << " value = " << it->second << endl;
    6. }
    7. cout << endl;
    8. }
    9. void test01()
    10. {
    11. //插入
    12. map<int, int> m;
    13. //第一种插入方式
    14. m.insert(pair<int, int>(1, 10));
    15. //第二种插入方式
    16. m.insert(make_pair(2, 20));
    17. //第三种插入方式
    18. m.insert(map<int, int>::value_type(3, 30));
    19. //第四种插入方式
    20. m[4] = 40;
    21. printMap(m);
    22. //删除
    23. m.erase(m.begin());
    24. printMap(m);
    25. m.erase(3);
    26. printMap(m);
    27. //清空
    28. m.erase(m.begin(), m.end());
    29. m.clear();
    30. printMap(m);
    31. }

    5.map的查找和统计

    1. find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
    2. count(key); //统计key的元素个数
    1. void test01()
    2. {
    3. map<int, int>m;
    4. m.insert(pair<int, int>(1, 10));
    5. m.insert(pair<int, int>(2, 20));
    6. m.insert(pair<int, int>(3, 30));
    7. //查找
    8. map<int, int>::iterator pos = m.find(3);
    9. if (pos != m.end())
    10. {
    11. cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
    12. }
    13. else
    14. {
    15. cout << "未找到元素" << endl;
    16. }
    17. //统计
    18. int num = m.count(3);
    19. cout << "num = " << num << endl;
    20. }

    总结:

    • 查找 --- find (返回的是迭代器)
    • 统计 --- count (对于map,结果为0或者1)

    6.map容器排序

    • map容器默认排序规则为 按照key值进行 从小到大排序,掌握如何改变排序规则
    1. #include
    2. #include
    3. using namespace std;
    4. class MyCompare {
    5. public:
    6. bool operator()(int v1, int v2) const {
    7. return v1 > v2;
    8. }
    9. };
    10. void test01()
    11. {
    12. //默认从小到大排序
    13. //利用仿函数实现从大到小排序
    14. map<int, int, MyCompare> m;
    15. m.insert(make_pair(1, 10));
    16. m.insert(make_pair(2, 20));
    17. m.insert(make_pair(3, 30));
    18. m.insert(make_pair(4, 40));
    19. m.insert(make_pair(5, 50));
    20. for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
    21. cout << "key:" << it->first << " value:" << it->second << endl;
    22. }
    23. }
    24. int main() {
    25. test01();
    26. system("pause");
    27. return 0;
    28. }

    写在最后:以上就是本篇文章的内容了,感谢你的阅读。如果感到有所收获的话可以给博主点一个赞哦。如果文章内容有遗漏或者错误的地方欢迎私信博主或者在评论区指出~

  • 相关阅读:
    【SpringBoot整合NoSql】-----ElasticSearch的安装与操作篇
    每日一练 | 华为认证真题练习Day114
    【每日训练】字符串中找出连续最长的数字串&&数组中出现次数超过一半的数字
    关于GPU显卡的介绍
    iOS调试技巧——使用Python 自定义LLDB
    从0开始学习JavaScript--JavaScript 数字与日期
    课时63:流程控制_case条件控制_语法解读
    RedisTemplate的使用之opsForValue
    Java并发编程—并发和并行、线程上下文
    腾讯不被看好?Prosus宣布减持股份,中国科技公司路在何方?
  • 原文地址:https://blog.csdn.net/weixin_63779802/article/details/134061140