zoukankan      html  css  js  c++  java
  • STL之map

    map中的key必须重载 "< " 运算符;map中value存放map类型时,不使用new:

     1 #include <iostream>
     2 #include <map>
     3 #include <string>
     4 
     5 using  namespace std;
     6 
     7 int main()
     8 {
     9     std::map<int, std::map<std::string, int>* > test_map;
    10     std::map<std::string, int> map_01 ;    
    11     for(int i=0; i<10; ++i){
    12         //map_01 = new std::map<std::string, int>;
    13         map_01.insert(make_pair("test_11", i));    #每次循环操作的是一个map,有点引用的意味
    14         std::cout <<"map address " <<&map_01 << std::endl;
    15         test_map.insert(make_pair(i, &map_01));    
    16     }   
    17 
    18     for(std::map<int, std::map<std::string, int>* >::iterator it=test_map.begin(); it!=test_map.end(); ++it){
    19         std::cout << "first " << it->first << "map_address "<< it->second << std::endl;
    20     }   
    21     
    22     /*  
    23     for(std::map<int, std::map<std::string, int>* >::iterator it = test_map.begin();
    24         it != test_map.end(); ++it){
    25         map_01 = it->second;
    26         delete map_01;
    27         map_01 = NULL;
    28     }
    29     */
    30 
    31     return 0;
    32 }

    运行结果:

     1 root@u18:~/cp/test# g++ map2.cpp  -g -Wall
     2 root@u18:~/cp/test# valgrind --tool=memcheck --leak-check=yes ./a.out
     3 ==19991== Memcheck, a memory error detector
     4 ==19991== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
     5 ==19991== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
     6 ==19991== Command: ./a.out
     7 ==19991== 
     8 map address 0x7ff0003c0
     9 map address 0x7ff0003c0
    10 map address 0x7ff0003c0
    11 map address 0x7ff0003c0
    12 map address 0x7ff0003c0
    13 map address 0x7ff0003c0
    14 map address 0x7ff0003c0
    15 map address 0x7ff0003c0
    16 map address 0x7ff0003c0
    17 map address 0x7ff0003c0
    18 first 0map_address 0x7ff0003c0
    19 first 1map_address 0x7ff0003c0
    20 first 2map_address 0x7ff0003c0
    21 first 3map_address 0x7ff0003c0
    22 first 4map_address 0x7ff0003c0
    23 first 5map_address 0x7ff0003c0
    24 first 6map_address 0x7ff0003c0
    25 first 7map_address 0x7ff0003c0
    26 first 8map_address 0x7ff0003c0
    27 first 9map_address 0x7ff0003c0
    28 ==19991== 
    29 ==19991== HEAP SUMMARY:
    30 ==19991==     in use at exit: 0 bytes in 0 blocks
    31 ==19991==   total heap usage: 21 allocs, 21 frees, 848 bytes allocated
    32 ==19991== 
    33 ==19991== All heap blocks were freed -- no leaks are possible
    34 ==19991== 
    35 ==19991== For counts of detected and suppressed errors, rerun with: -v
    36 ==19991== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

    使用new map情况:

     1 #include <iostream>
     2 #include <map>
     3 #include <string>
     4 
     5 using  namespace std;
     6 
     7 int main()
     8 {
     9     std::map<int, std::map<std::string, int>* > test_map;
    10     std::map<std::string, int>* map_01;
    11     for(int i=0; i<10; ++i){
    12         map_01 = new std::map<std::string, int>;
    13         std::cout <<"map address " <<map_01 << std::endl;
    14         test_map.insert(make_pair(i, map_01));
    15     }   
    16 
    17     for(std::map<int, std::map<std::string, int>* >::iterator it=test_map.begin(); it!=test_map.end(); ++it){
    18         std::cout << "first " << it->first << "map_address "<< it->second << std::endl;
    19     }   
    20     
    21     for(std::map<int, std::map<std::string, int>* >::iterator it = test_map.begin();
    22         it != test_map.end(); ++it){
    23         map_01 = it->second;
    24         delete map_01;
    25         map_01 = NULL;
    26     }   
    27 
    28     return 0;
    29 }

    运行情况:

     1 root@u18:~/cp/test# g++ map.cpp  -g -Wall
     2 root@u18:~/cp/test# valgrind --tool=memcheck --leak-check=yes ./a.out
     3 ==19960== Memcheck, a memory error detector
     4 ==19960== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
     5 ==19960== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
     6 ==19960== Command: ./a.out
     7 ==19960== 
     8 map address 0x5a02040
     9 map address 0x5a02120
    10 map address 0x5a02200
    11 map address 0x5a022e0
    12 map address 0x5a023c0
    13 map address 0x5a024a0
    14 map address 0x5a02580
    15 map address 0x5a02660
    16 map address 0x5a02740
    17 map address 0x5a02820
    18 first 0map_address 0x5a02040
    19 first 1map_address 0x5a02120
    20 first 2map_address 0x5a02200
    21 first 3map_address 0x5a022e0
    22 first 4map_address 0x5a023c0
    23 first 5map_address 0x5a024a0
    24 first 6map_address 0x5a02580
    25 first 7map_address 0x5a02660
    26 first 8map_address 0x5a02740
    27 first 9map_address 0x5a02820
    28 ==19960== 
    29 ==19960== HEAP SUMMARY:
    30 ==19960==     in use at exit: 0 bytes in 0 blocks
    31 ==19960==   total heap usage: 20 allocs, 20 frees, 960 bytes allocated
    32 ==19960== 
    33 ==19960== All heap blocks were freed -- no leaks are possible
    34 ==19960== 
    35 ==19960== For counts of detected and suppressed errors, rerun with: -v
    36 ==19960== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

     map的insert返回值:

     1 map<string, int> word_count;
     2 string word;
     3 while(cin>>word){
     4     //map insert的返回值类型
     5     pair<map<string, int>::iterator, bool> ret = 
     6         word_count.insert(make_pair(word, 1));
     7     if(!ret.second){
     8         ++ret.first->second;
     9     }   
    10 }
    11 
    12 //返回值为key为迭代器,value为bool值的map;插入成功与否由bool值来表示
  • 相关阅读:
    arcgis10.2转shp文件中文乱码问题解决方案
    Android Context作为参数传递this
    andriod inputbox
    andriod inputType
    《ArcGIS Runtime SDK for Android开发笔记》——(5)、基于Android Studio构建ArcGIS Android开发环境(离线部署)(转)
    终于理解了什么是LGPL
    产品经理如何与强势的技术沟通? 技术比较有资历,会以技术无法实现等方面的原因拒绝处理产品提出的需求。 你们是否遇到这样的技术? 产品懂技术的话,是不是会好一些,因为可以和技术说“行话”了,并且产品懂技术就不会被忽悠了。
    Core Dump总结
    LIBRARY_PATH是编译时候用的,LD_LIBRARY_PATH是程序运行是使用的
    如何禁止C++默认成员函数
  • 原文地址:https://www.cnblogs.com/chris-cp/p/4660765.html
Copyright © 2011-2022 走看看