• Code Snippet : C++ map 映射容器的使用样例


    #include <string>
    #include <map>
    #include <iostream>
    
    int main(void)
    {
        std::map<int,std::string> test_map;
    
    
        test_map[1] = "value1";
        test_map[2] = "value2";
    
        std::cout << "------ Test1: Read All -----" << std::endl;
    
        std::map<int, std::string>::iterator iter;
    
        for(iter = test_map.begin(); iter != test_map.end(); iter++)
        {
            std::cout << iter->first << ":" << iter->second << std::endl;
        }
    
        std::cout << "------ Test2: Insert Value pair -----" << std::endl;
    
        test_map.insert(std::pair<int,std::string>(3,"value3"));
    
        for(iter = test_map.begin(); iter != test_map.end(); iter++)
        {
            std::cout << iter->first << ":" << iter->second << std::endl;
        }
    
        std::cout << "------ Test2: Insert Value  map:value_type -----" << std::endl;
    
        test_map.insert(std::map<int, std::string>::value_type (4,"value4"));
    
        for(iter = test_map.begin(); iter != test_map.end(); iter++)
        {
            std::cout << iter->first << ":" << iter->second << std::endl;
        }
    
        std::cout << "------ Test2: Insert Value array[]  -----" << std::endl;
    
        test_map.insert(std::map<int, std::string>::value_type (6,"value6"));
    
        for(iter = test_map.begin(); iter != test_map.end(); iter++)
        {
            std::cout << iter->first << ":" << iter->second << std::endl;
        }
    
        std::cout << "------ Test2: Insert Value array[] override -----" << std::endl;
    
        test_map.insert(std::map<int, std::string>::value_type (6,"value7"));
    
        for(iter = test_map.begin(); iter != test_map.end(); iter++)
        {
            std::cout << iter->first << ":" << iter->second << std::endl;
        }
    
        std::cout << "------ Test3: Find Value -----" << std::endl;
    
        int a = 5;
    
        iter = test_map.find(a);
    
        if(iter == test_map.end())
        {
            std::cout << "Sorry," << a << " not found" << std::endl;
        }else
        {
            std::cout << "Found," << iter->first << ":" << iter->second << std::endl;
        }
    
        std::cout << "------ Test4: Erase Value -----" << std::endl;
    
        test_map.erase(4);
    
        for(iter = test_map.begin(); iter != test_map.end(); iter++)
        {
            std::cout << iter->first << ":" << iter->second << std::endl;
        }
    
        std::cout << "------ Test5: Check Insert Result fail -----" << std::endl;
    
        std::pair<std::map<int,std::string>::iterator,bool> result = test_map.insert(std::pair<int,std::string>(1,"override_value1"));
    
        std::cout << "result is " << result.second << std::endl;
    
        for(iter = test_map.begin(); iter != test_map.end(); iter++)
        {
            std::cout << iter->first << ":" << iter->second << std::endl;
        }
    
        std::cout << "------ Test5: Check Insert Result success -----" << std::endl;
    
        result = test_map.insert(std::pair<int,std::string>(7,"value7"));
    
        std::cout << "result is " << result.second << std::endl;
    
        for(iter = test_map.begin(); iter != test_map.end(); iter++)
        {
            std::cout << iter->first << ":" << iter->second << std::endl;
        }
    
    
        std::cout << "------ Test End -----" << std::endl;
    
        return 0;
    }

    运行结果

    ------ Test1: Read All -----
    1:value1
    2:value2
    ------ Test2: Insert Value pair -----
    1:value1
    2:value2
    3:value3
    ------ Test2: Insert Value  map:value_type -----
    1:value1
    2:value2
    3:value3
    4:value4
    ------ Test2: Insert Value array[]  -----
    1:value1
    2:value2
    3:value3
    4:value4
    6:value6
    ------ Test2: Insert Value array[] override -----
    1:value1
    2:value2
    3:value3
    4:value4
    6:value6
    ------ Test3: Find Value -----
    Sorry,5 not found
    ------ Test4: Erase Value -----
    1:value1
    2:value2
    3:value3
    6:value6
    ------ Test5: Check Insert Result fail -----
    result is 0
    1:value1
    2:value2
    3:value3
    6:value6
    ------ Test5: Check Insert Result success -----
    result is 1
    1:value1
    2:value2
    3:value3
    6:value6
    7:value7
    ------ Test End -----

     参考文献

    [1]. 史上最全的各种C++ STL容器全解析 - osc_ywuazj5t的个人空间 - OSCHINA

    [2]. C++中的STL中map用法详解 - Boblim - 博客园

    [3]. C++ STL map容器迭代器用法详解

     

     

     

  • 相关阅读:
    bzoj3786 星系探索
    [JSOI2008]火星人
    [NOI2005]维护数列
    [POI2008]砖块Klo
    郁闷的出纳员
    [HNOI2002]营业额统计
    [BZOJ1651][Usaco2006 Feb]Stall Reservations 专用牛棚
    [BZOJ2124]等差子序列
    [BZOJ3038]上帝造题的七分钟2
    [BZOJ1711][Usaco2007 Open]Dining吃饭
  • 原文地址:https://www.cnblogs.com/yqmcu/p/13335065.html
Copyright © 2020-2023  润新知