zoukankan      html  css  js  c++  java
  • C++ STL 之 map

    #include <iostream>
    #include <map>
    using namespace std;
    
    // map构造函数
    // map<T1, T2> mapTT;//map 默认构造函数:
    // map(const map &mp);//拷贝构造函数
    
    // map 赋值操作
    // map& operator=(const map &mp);//重载等号操作符
    // swap(mp);//交换两个集合容器
    // 
    // map 大小操作
    // size();//返回容器中元素的数目
    // empty();//判断容器是否为空
    // 
    // map 插入数据元素操作
    // map.insert(...); //往容器插入元素,返回 pair<iterator,bool>
    // map<int, string> mapStu;
    // // 第一种 通过 pair 的方式插入对象
    // mapStu.insert(pair<int, string>(3, "小张"));
    // // 第二种 通过 pair 的方式插入对象
    // mapStu.inset(make_pair(-1, "校长"));
    // // 第三种 通过 value_type 的方式插入对象
    // mapStu.insert(map<int, string>::value_type(1, "小李"));
    // // 第四种 通过数组的方式插入值
    // mapStu[3] = "小刘";
    // mapStu[5] = "小王";
    
    // map 删除操作
    // clear();//删除所有元素
    // erase(pos);//删除 pos 迭代器所指的元素,返回下一个元素的迭代器。
    // erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
    // erase(keyElem);//删除容器中 key 为 keyElem 的对组。
    // 
    // map 查找操作
    // find(key);//查找键 key 是否存在,若存在,返回该键的元素的迭代器;/若不存在,返回 map.end();
    // count(keyElem);//返回容器中 key 为 keyElem 的对组个数。对 map 来说,要么是 0,要么是 1。对
    // multimap 来说,值可能大于 1。
    // lower_bound(keyElem);//返回第一个 key<=keyElem 元素的迭代器。
    // upper_bound(keyElem);//返回第一个 key>keyElem 元素的迭代器。
    // equal_range(keyElem);//返回容器中 key 与 keyElem 相等的上下限的两个迭代器。
    
    void printMap(map<int, int>& myMap)
    {
        for (map<int, int>::iterator it = myMap.begin(); it != myMap.end(); it++)
        {
            cout << "key = " << (*it).first << " " << "value = " << (*it).second << endl;
        }
        cout << "---------------------" << endl;
    }
    
    // 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;
        }
        // 第二种插入方式
        mymap.insert(make_pair(20, 20));
        // 第三种插入方式
        mymap.insert(map<int, int>::value_type(30, 30));
        // 第四种插入方式
        // 如果发现key不存在,创建pair插入到map容器中,如果发现key存在,那么会修改key对应的value
        mymap[40] = 40;
        printMap(mymap);
        mymap[10] = 20;
        printMap(mymap);
        mymap[50] = 50;
        printMap(mymap);
        //如果通过[]方式去访问map中一个不存在key,那么map会将这个访问的key插入到map中,并且给value一个默认值
        cout << "mymap[60] = " << mymap[60] << endl;
        printMap(mymap);
    }
    
    // 自定义数据类型排序
    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;
        }
        cout << "---------------------" << endl;
    }
    
    // equal_range
    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(8);
        if (ret.first != mymap.end())
        {
            cout << "找到lower_bound" << endl;
        }
        else
        {
            cout << "没有找到!" << endl;
        }
        if (ret.second != mymap.end())
        {
            cout << "找到upper_bound" << endl;
        }
        else
        {
            cout << "没有找到" << endl;
        }
    }
    
    int main()
    {
        test01();
        test02();
        test03();
        getchar();
        return 0;
    }
  • 相关阅读:
    eclipse常用快捷键
    Sql server 问题诊断
    Oracle 表格大小分析
    VM虚拟机增加磁盘空间
    Linux搭建Nexus+Maven私人仓库
    Linux 下安装Git 版本管理工具 使用记录
    Jenkins 环境打建设 Linux
    Oracle 数据库用户表大小分析
    Windgb 其他常用命令
    Windbg 查内存占用
  • 原文地址:https://www.cnblogs.com/duxie/p/10927942.html
Copyright © 2011-2022 走看看