zoukankan      html  css  js  c++  java
  • STL关联式容器之map和multimap

    一,map和multimap的概念

    1.map和multimap的基本知识

    • map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对。它提供基于key的快速检索能力。
    • map中key值是唯一的,集合中的元素按照一定的顺序排列,元素插入过程是按照排序规则插入的,不能指定插入位置。
    • map的底层数据结构是红黑树变体的平衡二叉树。在插入和删除操作中比vector要快。
    • map可以直接存取key所对应得value,支持[]操作符,如map[key] = value;
    • multimap和map的区别:map支持唯一的键值对,每个键只能出现一次,而multimap中相同的键可以出现多次,multimap不支持[]操作符。
    • 要使用map必须要引入头文件# include<map>

    二,map和multimap的基本操作

    1.map和multimap的构造函数

    // map的无参构造函数
    map<string,string> m1;
    // multimap的无参构造函数
    multimap<string, string> m2;

    2.map的插入操作

    # include<iostream>
    # include<map>
    # include<string>
    using namespace std;
    
    int main()
    {
        // 定义map集合
        map<string, string> m;
        // 插入方式1,返回结果是是否插入成功
        pair<map<string,string>::iterator,bool> p1 = m.insert(pair<string, string>("Epic", "UnrealEngine"));
        // 插入方式2,返回结果是是否插入成功
        pair<map<string, string>::iterator, bool> p2 = m.insert(make_pair("Unity", "Unity3D"));
        // 插入方式3,返回结果是是否插入成功
        pair<map<string, string>::iterator, bool> p3 = m.insert(map<string, string>::value_type("Oracle", "Java"));
        // 插入方式4
        m["nokia"] = "Qt";
        // 插入方式4可以修改值
        m["nokia"] = "NewQt";
        // 遍历
        for (map<string, string>::iterator it = m.begin(); it != m.end(); it++)
        {
            cout << it->first << " = " << it->second << endl;
        }
    
        return 0;
    }

    3.map的遍历操作

    # include<iostream>
    # include<map>
    # include<string>
    
    using namespace std;
    
    int main()
    {
        // 创建映射
        map<string, string> m;
    
        // 插入元素
        m.insert(make_pair("王刚","张文婧"));
        m.insert(make_pair("吕布", "貂蝉"));
        m.insert(make_pair("刘备", "孙尚香"));
    
        // 正向遍历
        for (map<string, string>::iterator it = m.begin(); it != m.end(); it++)
        {
            cout << it->first << " = " << it->second << endl;
        }
    
        // 反向遍历
        for (map<string, string>::reverse_iterator it = m.rbegin(); it != m.rend(); it++)
        {
            cout << it->first << " = " << it->second << endl;
        }
    
        return 0;
    }

    4.map的删除操作

    # include<iostream>
    # include<map>
    # include<string>
    
    using namespace std;
    
    int main()
    {
        // 创建映射
        map<string, string> m;
        // 插入元素
        m.insert(make_pair("刘备1", "孙尚香1"));
        m.insert(make_pair("刘备2", "孙尚香2"));
        m.insert(make_pair("刘备3", "孙尚香3"));
        m.insert(make_pair("刘备4", "孙尚香4"));
        m.insert(make_pair("刘备5", "孙尚香5"));
        // 删除刘备4
        for (map<string, string>::iterator it = m.begin(); it != m.end(); )
        {
            if (it->first == "刘备4")
            {
                it = m.erase(it);
            }
            else {
                it++;
            }
        }
        // 遍历
        for (map<string, string>::iterator it = m.begin(); it != m.end(); it++)
        {
            cout << it->first << " = " << it->second << endl;
        }
        // 获取map的长度
        int size = m.size();
        cout << "size = " << size << endl;
        // 清空map
        m.clear();
        size = m.size();
        cout << "size = " << size << endl;
    
        return 0;
    }

    5.map的其他操作

    # include<iostream>
    # include<map>
    # include<string>
    
    using namespace std;
    
    int main()
    {
        map<string, string> m;
        /* 插入操作 */
        m.insert(make_pair("aaa", "AAA"));
        m.insert(make_pair("bbb", "BBB"));
        m.insert(make_pair("ccc", "CCC"));
        /* 统计,map的的可能结果为0或者1,multimap的结果是可能多变的 */
        int s = m.count("aaa");
        cout << "s = " << s << endl;
        /* 查找,根据key来查找,返回该key的迭代器 */
        map<string,string>::iterator it = m.find("aaa");
        cout << it->first << " = " << it->second << endl;
        
        return 0;
    }
  • 相关阅读:
    HIHO线段树(成段)
    HIHO 线段树(单点)
    POJ 3468
    HDU 1754
    HDU 1698
    HDU 5119
    HDU 1394
    HDU 1166
    DZY Loves Chessboard
    谷歌Cookies无法写入
  • 原文地址:https://www.cnblogs.com/metalsteel/p/6323505.html
Copyright © 2011-2022 走看看