zoukankan      html  css  js  c++  java
  • 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容器迭代器用法详解

     

     

     

  • 相关阅读:
    241. Different Ways to Add Parentheses java solutions
    89. Gray Code java solutions
    367. Valid Perfect Square java solutions
    46. Permutations java solutions
    116. Populating Next Right Pointers in Each Node java solutions
    153. Find Minimum in Rotated Sorted Array java solutions
    判断两颗树是否相同
    求二叉树叶子节点的个数
    求二叉树第k层的结点个数
    将二叉排序树转换成排序的双向链表
  • 原文地址:https://www.cnblogs.com/yqmcu/p/13335065.html
Copyright © 2011-2022 走看看