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

2021-04-14 21:28

阅读:361

标签:ret   begin   code   val   ras   value   read   ios   r++   

#include string>
#include 
#include int main(void)
{
    std::mapint,std::string> test_map;


    test_map[1] = "value1";
    test_map[2] = "value2";

    std::cout "------ Test1: Read All -----"  std::endl;

    std::mapint, std::string>::iterator iter;

    for(iter = test_map.begin(); iter != test_map.end(); iter++)
    {
        std::cout first ":" second  std::endl;
    }

    std::cout "------ Test2: Insert Value pair -----"  std::endl;

    test_map.insert(std::pairint,std::string>(3,"value3"));

    for(iter = test_map.begin(); iter != test_map.end(); iter++)
    {
        std::cout first ":" second  std::endl;
    }

    std::cout "------ Test2: Insert Value  map:value_type -----"  std::endl;

    test_map.insert(std::mapint, std::string>::value_type (4,"value4"));

    for(iter = test_map.begin(); iter != test_map.end(); iter++)
    {
        std::cout first ":" second  std::endl;
    }

    std::cout "------ Test2: Insert Value array[]  -----"  std::endl;

    test_map.insert(std::mapint, std::string>::value_type (6,"value6"));

    for(iter = test_map.begin(); iter != test_map.end(); iter++)
    {
        std::cout first ":" second  std::endl;
    }

    std::cout "------ Test2: Insert Value array[] override -----"  std::endl;

    test_map.insert(std::mapint, std::string>::value_type (6,"value7"));

    for(iter = test_map.begin(); iter != test_map.end(); iter++)
    {
        std::cout first ":" 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," " not found"  std::endl;
    }else
    {
        std::cout "Found," first ":" 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 first ":" second  std::endl;
    }

    std::cout "------ Test5: Check Insert Result fail -----"  std::endl;

    std::pair<:map>int,std::string>::iterator,bool> result = test_map.insert(std::pairint,std::string>(1,"override_value1"));

    std::cout "result is "  std::endl;

    for(iter = test_map.begin(); iter != test_map.end(); iter++)
    {
        std::cout first ":" second  std::endl;
    }

    std::cout "------ Test5: Check Insert Result success -----"  std::endl;

    result = test_map.insert(std::pairint,std::string>(7,"value7"));

    std::cout "result is "  std::endl;

    for(iter = test_map.begin(); iter != test_map.end(); iter++)
    {
        std::cout first ":" 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 -----

 

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

标签:ret   begin   code   val   ras   value   read   ios   r++   

原文地址:https://www.cnblogs.com/yqmcu/p/13335065.html


评论


亲,登录后才可以留言!