• map容器(20221125)


    一、map/multimap容器

    1、map基本概念

    map中所有元素都是pair;

    pair第一个元素为key(键值),起到索引的作用,第二个元素为value(实值);

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

    map/multimap实质都属于关联式容器 底层结构为二叉树

    优点:

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

    map/multimap区别:

    map中不允许有重复的key值,multimap中可以有。

    2、构造和赋值

    mapmp; //默认构造

    map(const map &mp); // 拷贝构造

    map& operator=(const map&mp); //赋值重载=操作符

    1. void printMap(mapint>&mp)
    2. {
    3.     for (mapint>::iterator it = mp.begin(); it != mp.end(); it++)
    4.     {
    5.         cout << "key = " << (*it).first << " value=" << (*it).second << endl;
    6.     }
    7. }
    8. void test01()
    9. {
    10.     mapint>mp;
    11.     mp.insert(pairint>("张三", 19)); //插入时必须要先创建一个对组
    12.     mp.insert(pairint>("张四", 20)); //插入时必须要先创建一个对组
    13.     mp.insert(pairint>("张五", 21)); //插入时必须要先创建一个对组
    14.     mp.insert(pairint>("张六", 10)); //插入时必须要先创建一个对组
    15.     printMap(mp);
    16.     //拷贝构造
    17.     mapint>mp1(mp);
    18.     cout << "mp1:" << endl;
    19.     printMap(mp1);
    20.     //赋值
    21.     mapint>mp2;
    22.     mp2 = mp1;
    23.     cout << "mp2:" << endl;
    24.     printMap(mp2);
    25. }

    2、map的大小和交换

    函数原型:

    size();

    empty();

    swap(st);

    用法与其它容器相同

    3、map插入和删除

    函数原型:

    insert(elem);

    clear();

    erase(pos);

    erase(beg,end);

    erase(key); //删除容器中值为key的元素

    用法与其他容器相同

    1. void test02()
    2. {
    3.     mapint>mp2;
    4.     mp2.insert(pairint>("B", 19)); //插入时必须要先创建一个对组
    5.     mp2.insert(pairint>("C", 20)); //插入时必须要先创建一个对组
    6.     mp2.insert(pairint>("A", 21)); //插入时必须要先创建一个对组
    7.     mp2.insert(pairint>("D", 10)); //插入时必须要先创建一个对组
    8.     mp2.insert(make_pair("E", 23));
    9.     //第三种插入
    10.     mp2.insert(mapint>::value_type("M", 20));
    11.     //第四种插入
    12.     mp2["F"] = 30;//不建议 ,可以用这个方式访问键值为key的实值value
    13.     printMap(mp2);
    14.     //删除
    15.     cout << "删除第一个元素" << endl;
    16.     mp2.erase(mp2.begin());
    17.     printMap(mp2);
    18.     cout << "删除key为M的元素" << endl;
    19.     mp2.erase("M"); //按key的方式删除
    20.     printMap(mp2);
    21.     cout << "清空元素" << endl;
    22.     mp2.clear();
    23.     printMap(mp2);
    24. }

    5、map的查找与统计

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

    count(key);//统计key的元素个数

    用法与set的查找与统计相同。

    1. void test03()
    2. {
    3.     mapint>mp2;
    4.     mp2.insert(pairint>("B", 19)); //插入时必须要先创建一个对组
    5.     mp2.insert(pairint>("C", 20)); //插入时必须要先创建一个对组
    6.     mp2.insert(pairint>("A", 21)); //插入时必须要先创建一个对组
    7.     mp2.insert(pairint>("D", 10)); //插入时必须要先创建一个对组
    8.     mp2.insert(make_pair("E", 23));
    9.     //第三种插入
    10.     mp2.insert(mapint>::value_type("M", 20));
    11.     //第四种插入
    12.     mp2["F"] = 30;//不建议 ,可以用这个方式访问键值为key的实值value
    13.     //查找
    14.     mapint>::iterator pos = mp2.find("B");//返回的是迭代器 用迭代器接收
    15.     if (pos != mp2.end())
    16.     {
    17.         cout << "元素存在,其实值为:"<< (*pos).second<< endl;
    18.         cout << "其个数为: " << mp2.count("B") << endl;
    19.     }
    20.     else
    21.     {
    22.         cout << "元素不存在!" << endl;
    23.     }
    24. }

    6、map容器的排序规则

    1. class MyCompare {
    2. public:
    3.     bool operator()(string v1, string v2)const //重载()  需要加一个const 否则会报错
    4.     {
    5.         return v1 > v2;
    6.     }
    7. };
    8. void test04()
    9. {
    10.     mapint>mp2;
    11.     mp2.insert(pairint>("B", 19)); //插入时必须要先创建一个对组
    12.     mp2.insert(pairint>("C", 20)); //插入时必须要先创建一个对组
    13.     mp2.insert(pairint>("A", 21)); //插入时必须要先创建一个对组
    14.     mp2.insert(pairint>("D", 10)); //插入时必须要先创建一个对组
    15.     mp2.insert(make_pair("E", 23));
    16.     mp2.insert(make_pair("E", 28));
    17.     //第三种插入
    18.     mp2.insert(mapint>::value_type("M", 20));
    19.     //第四种插入
    20.     mp2["F"] = 30;//不建议 ,可以用这个方式访问键值为key的实值value
    21.     //查找
    22.     mapint>::iterator pos = mp2.find("B");//返回的是迭代器 用迭代器接收
    23.     if (pos != mp2.end())
    24.     {
    25.         cout << "元素存在,其实值为:" << (*pos).second << endl;
    26.         cout << "其个数为: " << mp2.count("B") << endl;
    27.     }
    28.     else
    29.     {
    30.         cout << "元素不存在!" << endl;
    31.     }
    32.     cout << "默认排序为:" << endl;
    33.     printMap(mp2);
    34.     cout << "自己定义排序插入后:" << endl;
    35.     mapint,MyCompare>mp3;
    36.     mp3.insert(pairint>("B", 19));
    37.     mp3.insert(pairint>("C", 20));
    38.     mp3.insert(pairint>("A", 21));
    39.     mp3.insert(pairint>("D", 10));
    40.     mp3.insert(make_pair("E", 23));
    41.     mp3.insert(make_pair("E", 28));
    42.     mp3.insert(mapint>::value_type("M", 20));
    43.     mp3["F"] = 30;//不建议 ,可以用这个方式访问键值为key的实值value
    44.     for (mapint, MyCompare>::iterator it = mp3.begin(); it != mp3.end(); it++)
    45.     {
    46.         cout << "key为: " <first << " value为:" << (*it).second << endl;
    47.     }
    48. }

    也可以不用定义MyCompare类,直接用greater ,其中string表示比较string的大小

    1. mapint,greater>mp3;
    2.     mp3.insert(pairint>("B", 19));
    3.     mp3.insert(pairint>("C", 20));
    4.     mp3.insert(pairint>("A", 21));
    5.     mp3.insert(pairint>("D", 10));
    6.     mp3.insert(make_pair("E", 23));
    7.     mp3.insert(make_pair("E", 28));
    8.     mp3.insert(mapint>::value_type("M", 20));
    9.     mp3["F"] = 30;//不建议 ,可以用这个方式访问键值为key的实值value
    10.     for (mapint>::iterator it = mp3.begin(); it != mp3.end(); it++)
    11.     {
    12.         cout << "key为: " <first << " value为:" << (*it).second << endl;
    13. }

    注意map容器只能通过键值大小来进行排序,不能通过实值的大小来进行排序,若需要通过实值大小排序,可以转为其他容器来实现,如vecter, vecter>。

  • 相关阅读:
    idea常用快捷键 idea搜索快捷键
    Hystrix原理
    APScheduler 环境与基础
    win10中ros与ubuntu中ros通信
    牛客——OR36 链表的回文结构(C语言,配图,快慢指针)
    我赢助手小技巧:学会这三招,爆款内容视频完播率提高50%(中)
    线上项目源码安全性处理方案
    大数据-玩转数据-Python Sftp Mysql 数据
    PHP 5 Filesystem 函数
    react native 安卓打包报这个错误> Task :app:mergeDexRelease
  • 原文地址:https://blog.csdn.net/qq_60143666/article/details/128040776