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值来表示
原文地址:https://www.cnblogs.com/chris-cp/p/4660765.html