zoukankan      html  css  js  c++  java
  • map/multimap

    一、map/multimap特性

    map相对于set的区别,map具有键值和实值,所有元素根据键值自动排序,pair的第一元素被称为键值,第二元素被称为实值。map也是以红黑树为底层实现机制。

    问:通过map的迭代器可以修改map的键值吗?

    答:不能,键值关系到容器内元素的排序规则,任意改变键值会破坏容器的排列规则,但是可以改变实值。

    map和multimap区别在于,map不允许相同key值存在,multimap则允许相同key值存在。

    二、map常用API

    1、map构造函数

    2、map赋值操作

    3、map大小操作

    4、map插入数据元素操作

    注意:

    • 前三种方法,采用的是insert()方法,该方法返回值为pair<iterator,bool>
    • 第四种方法非常直观,但存在一个性能的问题。插入3时,先在mapStu中查找主键为3的项,若没发现,则将一个键为3,值为初始化值的对组插入到mapStu中,然后再将值修改为“小刘”。若发现已存在3这个键,则修改这个键对应的value。
    • string strName = mapStu[2];//取操作或插入操作
    只有当mapStu存在2这个键时才是正确的取操作,否则会自动插入一个实例,键为2,值为初始化值。

    5、map删除操作

    6、map查找操作

    三、案例

    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    #include <map>
    using namespace std;
    
    //map容器初始化
    void test01()
    {
        //map容器模板参数,第一个参数 key的类型,第二个参数 value的类型
        map<int,int> mymap;
    
        //插入参数 pair.first:key值, pair.second:value值
        //方法一
        pair<map<int, int>::iterator, bool> ret = mymap.insert(pair<int, int>(10, 10));
        if (ret.second)
        {
            cout << "第一次插入成功!" << endl;
        }
        else
        {
            cout << "插入失败!" << endl;
        }
        ret = mymap.insert(pair<int, int>(10, 20));
        if (ret.second)
        {
            cout << "第二次插入成功!" << endl;
        }
        else
        {
            cout << "插入失败!" << endl;
        }
        //第一次插入成功!
        //插入失败!
        //如果插入已存在相同键值的pair,则插入失败
    
        //方法二
        mymap.insert(make_pair(20, 20));
        //方法三
        mymap.insert(map<int, int>::value_type(30, 30));
        //方法四
        mymap[40] = 40;
        mymap[10] = 20;
        mymap[50] = 50;
        /*key: 10 value : 20
          key : 20 value : 20
          key : 30 value : 30
          key : 40 value : 40
          key : 50 value : 50*/
        //如果key不存在,创建pair插入到map容器中;
        //如果发现key存在,那么会修改key对应的value
    
        //打印
        for (map<int, int>::iterator it = mymap.begin();it != mymap.end();it++)
        {
            //*it取出来的是第一个pair
            cout << "key:" << (*it).first << " value:" << it->second << endl;
        }
    
        cout << "mymap[60]:" << mymap[60] << endl;
        //    mymap[60] : 0
    
        for (map<int, int>::iterator it = mymap.begin();it != mymap.end();it++)
        {
            //*it取出来的是第一个pair
            cout << "key:" << (*it).first << " value:" << it->second << endl;
        }
        /*key : 10 value : 20
          key : 20 value : 20
          key : 30 value : 30
          key : 40 value : 40
          key : 50 value : 50
          key : 60 value : 0*/
        
        //如果通过[]方式去访问map中一个不存在的key,
        //那么map会将这个访问的key插入到map中,并且给value一个默认值
    
    }
    
    class MyKey
    {
    public:
        MyKey(int index, int id)
        {
            this->mIndex = index;
            this->mID = id;
        }
    public:
        int mIndex;
        int mID;
    };
    
    struct mycompare
    {
        bool operator()(MyKey key1, MyKey key2)
        {
            return key1.mIndex < key2.mIndex;
        }
    };
    void test02()
    {
        map<MyKey, int, mycompare> mymap;//自动排序,自定义数据类型,需自行设置排序规则
        mymap.insert(make_pair(MyKey(1, 2), 10));
        mymap.insert(make_pair(MyKey(4, 5), 20));
    
        for (map<MyKey, int, mycompare>::iterator it = mymap.begin();it != mymap.end();it++)
        {
            cout << it->first.mIndex << ":" << it->first.mID << "=" << it->second << endl;
    
        }
    }
    
    void test03()
    {
        map<int, int> mymap;
        mymap.insert(make_pair(1, 4));
        mymap.insert(make_pair(2, 5));
        mymap.insert(make_pair(3, 6));
    
        pair<map<int, int>::iterator, map<int, int>::iterator> ret = mymap.equal_range(2);
        if (ret.first->second)
        {
            cout << "找到lower_bound!" << endl;
        }
        else
        {
            cout << "没有找到!";
        }
    
        if (ret.second->second)
        {
            cout << "找到upper_bound!" << endl;
        }
        else
        {
            cout << "没有找到!";
        }
    }
    
    int main(void)
    {
        //test01();//前三种方法效果是一样的,与第四种不同
        //test02();
        test03();
        return 0;
    }
  • 相关阅读:
    C#获取HTML文件指定DIV内容
    剔除editor编辑器中的HTML标签
    ASP.NET MVC 在WebService中Token的使用方法
    MVC如何在单独的类库中添加区域
    原来写插件还可以选MEF
    EF CodeFirst学习
    .Net EF 学习之model first
    sql分页存储过程,带求和、排序
    软实力
    微软的XML可视化编辑器:XML Notepad 2007
  • 原文地址:https://www.cnblogs.com/yuehouse/p/10114963.html
Copyright © 2011-2022 走看看