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;
}
posted @ 2021-08-19 15:24  编程驴子  阅读(148)  评论(0编辑  收藏  举报