zoukankan      html  css  js  c++  java
  • stl(14)map容器multimap容器

    1.map容器底层结构和set容器类似。但是map容器中的没有元素是一个键值对,即一个pair类型的元素,first为元素的键,second为元素的值。

    2.map容器中会按照元素的键值来进行排序,并且map容器中不允许出现相同的键值的元素,multimap容器中允许出现键值一样的元素

    3.不能通过map的迭代器来修改元素的键值,但是可以修元素的value值。因为map容器是使用键值排序的而不是value值。

    4.map的构造,赋值,swap,empty,size函数的使用与set容器一致

    5.insert向map容器中插入数据,inset的返回值是一个

    (1)几种不同的插入方式

    std::map<int,int> map;

    map.insert(std::pair<int,int>(1,10));

    map.insert(std::make_pair(2,20));

    map.insert(std::map<int,int>::value_type(3,30));

    map[4]=40;

    遍历:

    for (std::map<int, int>::iterator it = map.begin(); it != map.end();it++) {
    std::cout<<(*it).first<<" "<<it->second <<std::endl;
    }

    6.clear和erase的用法也和set类似,但是map容器通过erase传入一个元素进去删除与该元素相同的元素的时候,这里只传入key值,会删除map容器中key值相等的元素

    std::map<int,int> map;

    map.insert(std::pair<int,int>(1,10));

    map.erase(1);

    7.同样find,count,lower_bound,upper_bound,equal_range的用法与set容器,但是针对的都是键进行查找操作。

    8.修改map容器默认的排序规则

    class MyCompare{

    public:

      bool operator()(int v1,intv2){//重载操作符"()"

        return v1>v2;

      }

    }

    std::map<int,int,MyCpmpare> map;

    map.insert(std::pair<int,int>(1,10));

    map.insert(std::pair<int,int>(2,10));

    map.insert(std::pair<int,int>(3,10));

  • 相关阅读:
    js秒换成天时分
    vuex
    匹配iPhoneX
    ASP 解析json
    WPF 实现多语言支持
    c# 用DotNetZip来解压/压缩文件
    WPF UI Close button
    VB 获取所有窗体菜单信息
    ASP/ASP.NET/VB6文件上传
    c# 上传图片到一个外链相册服务器
  • 原文地址:https://www.cnblogs.com/maycpou/p/14348939.html
Copyright © 2011-2022 走看看