[STL] map,multimap,unordered_map基本用法

map的特性是,所有元素都会根据元素的键值自动被排序。map的所有元素都是pair,同时拥有键值(key)和实值(value)。pair的第一元素被视为键值,第二元素被视为实值。map不允许两个元素拥有相同的键值
multimap的特性以及用法与map完全相同,唯一的差别在于它允许键值重复。unordered_map与map的区别就在于不会根据key的大小进行排序.

1.插入数据的方法

 1 #include <map>
 2 #include <string>
 3 #include <iostream>
 4 using namespace std;
 5 int main()
 6 {
 7     map<int, string> Map1,Map2;
 8     //insert函数插入数据
 9     Map1.insert(pair<int, string>(1, "A"));
10     Map1.insert(pair<int, string>(2, "B"));
11     Map1.insert(pair<int, string>(3, "C"));
12     map<int, string>::iterator  iter;
13     cout<<"Map1:"<<endl;
14     for(iter = Map1.begin(); iter != Map1.end(); iter++)
15     {
16         cout<<iter->first<<" "<<iter->second<<"     ";
17     }
18 
19     //数组方式插入数据
20     Map2[1] = "A";
21     Map2[2] = "B";
22     Map2[3] = "C";
23     cout<<endl<<"Map2:"<<endl;
24     for(iter = Map2.begin(); iter != Map2.end(); iter++)
25     {
26         cout<<iter->first<<" "<<iter->second<<"     ";
27     }
28     return 0;
29 }

使用insert函数和使用数组插入数据的区别就在于:

用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作

是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值。

2.数据的查找(包括判定这个关键字是否在map中出现)

(1)若要实现判断一个key是否存在,如果存在就输出,不存在就不输出的功能,则可以使用count函数。count函数的功能是统计关键字出现的次数。map对于关键字来说是唯一的,也就是说在map中不存在等价的两个(以上)元素,因此某个元素在map/set中出现的次数最多只能为1,用count得到的结果不是0就是1。

(2)用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,程序说明:

 1 #include <map>
 2 #include <string>
 3 #include <iostream>
 4 using namespace std;
 5 int main()
 6 {
 7     map<int, string> Map;
 8     //insert函数插入数据
 9     Map.insert(pair<int, string>(1, "A"));
10     Map.insert(pair<int, string>(2, "B"));
11     Map.insert(pair<int, string>(3, "C"));
12     map<int, string>::iterator  iter;
13     //返回key值为1的迭代器位置
14     iter = Map.find(1);
15     if(iter != Map.end())
16         cout<<"Find,the value is "<<iter->second<<endl;
17     else
18         cout<<"Not find"<<endl;
19     return 0;
20 }

3.数据的清空与判空
清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map
4. 数据的删除
这里要用到erase函数,它有三个重载了的函数,下面在例子中详细说明它们的用法

 1 #include <map>
 2 #include <string>
 3 #include <iostream>
 4 using namespace std;
 5 int main()
 6 {
 7     map<int, string> Map;
 8     //insert函数插入数据
 9     Map.insert(pair<int, string>(1, "A"));
10     Map.insert(pair<int, string>(2, "B"));
11     Map.insert(pair<int, string>(3, "C"));
12     map<int, string>::iterator  iter;
13 
14     //用迭代器删除key为2的数据
15     iter = Map.find(2);
16     Map.erase(iter);
17     //打印输出
18     for(iter = Map.begin(); iter != Map.end(); iter ++)
19     {
20         cout<<iter->first<<" "<<iter->second<<"     ";
21     }
22     cout<<endl;
23 
24     Map.insert(pair<int, string>(2, "B"));
25 
26     //用关键字删除key为2的数据
27     int n = Map.erase(2);//删除成功返回1,否则返回0
28     if(n == 1)
29     {
30         //打印输出
31         for(iter = Map.begin(); iter != Map.end(); iter ++)
32         {
33             cout<<iter->first<<" "<<iter->second<<"     ";
34         }
35     }
36     cout<<endl;
37 
38     Map.insert(pair<int, string>(2, "B"));
39 
40     //用迭代器,成片的删除
41     //把整个map清空
42     Map.erase(Map.begin(),Map.end());
43     for(iter = Map.begin(); iter != Map.end(); iter ++)
44     {
45         cout<<iter->first<<" "<<iter->second<<"     ";
46     }
47     return 0;
48 }

原文地址:https://www.cnblogs.com/lca1826/p/6349905.html