STL的map

  本篇文章之所以诞生,是由于在刷PAT时需要掌握STL模板库,所以特意测试学习了常用的方法。仅适用于G++环境下的单线程单文档情况。对于多文档多线程情况不保证正确性。如果你是只想在数据结构与算法比赛中多拿一些AC而学习STL,小码农自认它还是挺好的。如果是项目需要而学习STL,还是建议读官方文档。
  如果你觉得博文状态下阅读费劲,可以去我的github下载源码。如果出现对不齐的情况,把编辑器Tab键缩进设置成4个就可以了。欢迎指点评论。

#include<bits/stdc++.h>
using namespace std;
/*
    下面的注释: 
        1)DT : data type 
        2)遍历说明:以集合名开头的都是遍历本次测试使用到的集合的结果,
                    由于太多cout会使调理不清晰,便直接放上输出结果。 
*/
bool cmp(const string &e1, const string &e2){ return e1 > e2; } 
int main()
{
    /*
        1、创建
            1)默认创建:                 map<DT1, DT2> m1;
            2)拷贝创建:                 map<DT1, DT2> m2(m1);
            3)根据其他map的某一段创建:    map<DT1, DT2> m3(iterator1, iterator2); 
    */
    map<string, int> m1;
    m1["str1"] = 1;
    m1["str2"] = 2;
    m1["str3"] = 3;
    map<string, int> m2(m1);
    map<string, int> m3(m1.begin(), m1.end());
    /*Console:          m1: str1=1  str2=2  str3=3
                        m2: str1=1  str2=2  str3=3
                        m3: str1=1  str2=2  str3=3              */

    /*
        2、插入
            1)通过运算符[]、=:            m[key] = value; 
                key值相同时,不能插入 
            2)使用insert():           pair<map<DG1, DT2>::iterator, bool> insert(pair<DT1, DT2>(key, value));
                key相同时,后插入的value覆盖前者 
    */
    m1.insert(pair<string, int>("str3", 4));
    m1["str2"] = 4; 
    cout << m1["str3"] << " " << m1["str2"] << endl;
    /*Console:                  3 4             
                            m1: 1 4 3                                               */

    /*
        3、删除
            1)删除指定key值:         int erase(DT1 key);
                成功,即key存在返回1;失败,即key不存在返回0 
            2)删除迭代器之指向的数据:  void erase(iterator position);
            3)清空此map:               void clear(); 
    */
    int e1 = m2.erase("str1");
    m2.erase(m2.begin());
    cout << e1 << endl;
    /*Console:          1
                    m2:str3=3            */

    /*
        4、获取元素
            1)重装的操作符:                   operator[]
            2)at()函数:                       DT2 at(key);
            3)获得指向首位置之前的迭代器:        iterator begin();
            4)获得指向尾位置之后的迭代器:        iterator end();
    */
    int v1 = m1.at("str1");
    cout << v1 << endl;
    /*Console:          1                        */

    /*
        5、查找
            1)返回迭指向该数据之前位置的迭代器:         iterator find(DT1 key);
                    找不到时返回指向尾元素之后的迭代器 
            2)返回是否存在:                               int count(DT1 key); 
    */
    map<string, int>::iterator it5 = m1.find("str3");
    int c1 = m1.count("str1");
    cout << it5->second << " " << c1 << endl;
    /*Console:           3 1                         */

    /*
        6、自定义key的排序规则
            使用decltype,传入自定义排序规则  
    */
    map<string, int, decltype(cmp)*> m6(cmp);
    m6["str1"] = 1;
    m6["str2"] = 2;
    /*Console:           m6:str2=2  str1=1                       */

    /*
        7、遍历
            使用迭代器:              for(map<string, int>::iterator it = m.begin(); it != m.end(); it++)
    */
    for(map<string, int>::iterator it = m1.begin(); it != m1.end(); it++)
        cout << it->first << "=" << it->second << "  ";
    cout << endl;
    /*Console:           str2=2  str1=1                      */

    /*
        8、其他:
            1)赋值号:              map& operator= (const map& x);
                    返回指向当前set的指针 
            2)数据个数:         int size();
            3)等于号:              operator==
            4)交换:               void swap(map<DT1, DT2> &ano); 
    */
    map<string, int> m8 = m1;
    cout << m8.size() << " " << (m8 == m1) << endl;
    /*Console:           3 1                         */

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38206090/article/details/81345105