1. 程式人生 > 其它 >c++ map基本知識

c++ map基本知識

一維map基本操作

#include <iostream>
#include <string>
#include <map>
//#include <utility> // 使用pair, 需要include utility, 如果使用make_pair則不需要include.

int main()
{
    using std::cout     ;
    using std::endl     ;
    using std::string   ;
    using std::map      ;
    //using std::pair     ;
    using std::make_pair; //使用make_pair

    //1. 建立物件
    typedef map<int, string> MAP_INT_STR;
    MAP_INT_STR person0; //可以把map模板進行型別定義, 方便建立物件

    //2. 新增資料
    //person0.insert(pair<int, string>(1, "NameA")); //插入pair資料, 需要指定資料型別
    person0.insert(make_pair(1, "NameA")); //插入make_pair資料, 不需要指定資料型別
    person0.insert(map<int, string>::value_type(2, "NameB")); //插入value_type資料
    person0[4] = "NameC"; //直接給新key賦值
    person0[3] = "NameD"; //直接給新key賦值

    //3. 遍歷map資料
    //3.1 向前迭代器遍歷
    //    遍歷時, map自動為key按升序排序, 所以會先訪問到3->Jerry, 後訪問到4->Kate.
    cout << "iterator" << endl;
    for(MAP_INT_STR::iterator it=person0.begin(); it!=person0.end(); it++)
    {
        cout << "    " << it->first << ": " << it->second << endl;
    }

    //3.2 反向迭代器遍歷
    cout << "reverse_iterator" << endl;
    for(MAP_INT_STR::reverse_iterator it=person0.rbegin(); it!=person0.rend(); it++)
    {
        cout << "    " << it->first << ": " << it->second << endl;
    }

    //4. 查詢元素
    MAP_INT_STR::iterator it0_find = person0.find(2); //使用find
    if (it0_find==person0.end()) //如果沒找到, 則返回尾部迭代器
        cout << "not find key 2." << endl;
    else //如果找到了, 則返回對應元素的迭代器
        cout << "find key 2, value is " << it0_find->second << endl;
    
    //5. 刪除元素
    person0.erase(3); //刪除key==3的元素
    person0.erase(person0.begin(), person0.end()); //刪除所有元素
    person0.clear(); //刪除所有元素

    //6. map.swap(), 交換兩個map容器
    MAP_INT_STR person1;
    person0.swap(person1); //person0和person1的key-value互換
    swap(person0, person1); //也可以直接使用swap函式

    //7. map不能用sort函式, 遍歷時自動按key升序進行

    return 0;
}

map的常用操作函式

 
    //8. 基本操作函式
    person0.begin();    //返回指向頭部的迭代器
    person0.clear();    //刪除所有元素
    person0.count();    //??
    person0.empty();    //map為空則返回true
    person0.end();      //返回指向尾部的迭代器
    person0.erase();    //刪除元素
    person0.find();     //查詢元素
    person0.insert();   //插入元素
    person0.max_size(); //可容納的最大元素個數
    person0.rbegin();   //返回指向尾部的逆向迭代器
    person0.rend();     //返回指向頭部的逆向迭代器
    person0.swap();     //交換兩個map

多維map的使用

#include <iostream>
#include <string>
#include <sstream>
#include <map>

using std::cout;
using std::endl;
using std::string;
using std::stringstream;
using std::map;

typedef map<string, string> MAP_S  ; //一維map
typedef map<string, MAP_S> MAP_SS  ; //二維map
typedef map<string, MAP_SS> MAP_SSS; //三維map

void print_map_sss(MAP_SSS ma_in) //遍歷三維map, 並列印其key-value
{
    MAP_SSS::iterator p3; //三維map迭代器
    MAP_SS::iterator  p2; //二維map迭代器
    MAP_S::iterator   p1; //一維map迭代器

    for(p3 = ma_in.begin(); p3!=ma_in.end(); p3++) //遍歷第一維元素
    {
        cout << p3->first << endl;
        for(p2 = p3->second.begin(); p2!=p3->second.end(); p2++) //遍歷第二維元素
        {
            cout << "    " << p2->first << endl;
            for(p1 = p2->second.begin(); p1!=p2->second.end(); p1++) //遍歷第三維元素
            {
                cout << "        " << p1->first << ": " << p1->second << endl;
            }
        }
    }
}

int main()
{
    MAP_SSS ma; //建立一個三維map物件

    ma["0"]["0"]["0"] = "000"; //可以直接給三維map賦值, 如果當前key不存在, 則直接建立並賦值
    ma["0"]["0"]["1"] = "001";
    ma["0"]["1"]["0"] = "010";
    ma["0"]["1"]["1"] = "011";
    ma["1"]["0"]["0"] = "100";
    ma["1"]["0"]["1"] = "101";
    ma["1"]["1"]["0"] = "110";
    ma["1"]["1"]["1"] = "111";

    print_map_sss(ma);

    return 0;
}