[C++学习笔记11]map

  1. map简介
      使用map包含map类所在的头文件
        #include <map>
      定义一个map对象
        map<string, int> mapTest;
      容器类型
        关联 key/value
      实现方式
        红黑树
  2. 插入数据
      插入数据的四种方式
        
    mapTest["aaa"] = 100;

        mapTest.insert(map<string, int>::value_type("bbb", 200));
        mapTest.insert(pair<string, int>("ccc", 300));
        mapTest.insert(make_pair("ddd", 400));

      区别
        第一种可以通过再次书写修改指定key的value,而后三种不行。
    #include <map>
    #include <iostream>
    #include <string>
    using namespace std;
    
    void show_map(const map<string, int> &mapTest)
    {
        map<string, int>::const_iterator it;
        for ( it = mapTest.begin(); it != mapTest.end(); ++it)
        {
            cout << it->first << " " << it->second << endl;
        }
        cout << endl;
    }
    
    int main(void)
    {
        // map 是按照key从小到大排序,所以对key要求是必须重载了<运算符
        // 与你插入的顺序无关
        map<string, int> mapTest;
        mapTest["aaa"] = 100; // 第一种插入数据的方式,可以通过mapTest["aaa"] = 200;覆盖前面的值
        mapTest.insert(map<string, int>::value_type("bbb", 200)); // 这几种都不能覆盖前面的值
        mapTest.insert(pair<string, int>("ccc", 300));
        mapTest.insert(make_pair("ddd", 400));
    
        show_map(mapTest);
    
        return 0;
    }

  3. 查找与修改
      查找修改的两种方式
        mapTest["aaa"] = 200;

        map<string, int>::iterator it = mapTest.find("bbb");
        if (it != mapTest.end())
          it->second = 300;

    #include <map>
    #include <iostream>
    #include <string>
    using namespace std;
    
    void show_map(const map<string, int> &mapTest)
    {
        map<string, int>::const_iterator it;
        for (it = mapTest.begin(); it != mapTest.end(); ++it)
        {
            cout << it->first << " " << it->second << endl;
        }
        cout << endl;
    }
    
    int main(void)
    {
        // map 是按照key从小到大排序,所以对key要求是必须重载了<运算符
        // 与你插入的顺序无关
        map<string, int> mapTest;
        mapTest["aaa"] = 100; // 第一种插入数据的方式,可以通过mapTest["aaa"] = 200;覆盖前面的值
        mapTest.insert(map<string, int>::value_type("bbb", 200)); // 这几种都不能覆盖前面的值
        mapTest.insert(pair<string, int>("ccc", 300));
        mapTest.insert(make_pair("ddd", 400));
    
        mapTest["aaa"] = 200; // 修改方式1
        map<string, int>::iterator it = mapTest.find("bbb"); // 修改方式2
        if (it != mapTest.end())
            it->second = 300;
        else
            cout << "not found." << endl;
    
    
        show_map(mapTest);
    
        return 0;
    }

  4. 删除
      删除的两种方式
        mapTest.erase("aaa");

        map<string, int>::const_iterator it = mapTest.find("bbb");
        if (it != mapTest.end())
          mapTest.erase(it);

    #include <map>
    #include <iostream>
    #include <string>
    using namespace std;
    
    void show_map(const map<string, int> &mapTest)
    {
        map<string, int>::const_iterator it;
        for (it = mapTest.begin(); it != mapTest.end(); ++it)
        {
            cout << it->first << " " << it->second << endl;
        }
        cout << endl;
    }
    
    int main(void)
    {
        // map 是按照key从小到大排序,所以对key要求是必须重载了<运算符
        // 与你插入的顺序无关
        map<string, int> mapTest;
        mapTest["aaa"] = 100; // 第一种插入数据的方式,可以通过mapTest["aaa"] = 200;覆盖前面的值
        mapTest.insert(map<string, int>::value_type("bbb", 200)); // 这几种都不能覆盖前面的值
        mapTest.insert(pair<string, int>("ccc", 300));
        mapTest.insert(make_pair("ddd", 400));
    
        mapTest.erase("aaa"); // 删除方式1
        map<string, int>::const_iterator it = mapTest.find("bbb"); // 删除方式2
        if (it != mapTest.end())
            mapTest.erase(it);
        else
            cout << "not found" << endl;
    
        show_map(mapTest);
    
        return 0;
    }

     

 

posted @ 2015-05-23 22:00  IFPELSET  阅读(210)  评论(0编辑  收藏  举报