STL之map

简介: map中的key必须重载 "< " 运算符;map中value存放map类型时,不使用new: 1 #include 2 #include 3 #include 4 5 using namespace std; 6 7 int main() 8 { ...

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值来表示

 

相关文章
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
408 2
|
存储 算法 C++
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
833 73
|
C++ 容器
【C++】红黑树模拟实现STL中的map与set
【C++】红黑树模拟实现STL中的map与set
|
存储 C++ 索引
C++基础知识(八:STL标准库 Map和multimap )
C++ 标准模板库(STL)中的 map 容器是一种非常有用的关联容器,用于存储键值对(key-value pairs)。在 map 中,每个元素都由一个键和一个值组成,其中键是唯一的,而值则可以重复。
547 1
|
存储 编译器 C++
|
存储 算法 C++
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
|
C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
215 0
|
存储 C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
460 0
|
C++ 索引 容器
黑马c++ STL部分 笔记(9) map/multimap容器
黑马c++ STL部分 笔记(9) map/multimap容器
|
存储 C++ 容器
【STL】map和set的原理及其使用
【STL】map和set的原理及其使用