没有输出的输入是不完整的

0%

multimap的简单使用

本文为c++容器系列第七篇.
主要测试了multimap相关的方法,主要作为代码备份方便之后遗忘时查询。
map,multimap,unordered_map,unordered_multimap仅仅有稍许差别,方法和multimap基本类似,所以不再另行写文章啦。

写在最前面

  1. map和multimap的差别仅仅在于multimap插入的key值可以相同,其他操作都类似。所有不再单独另写文章啦。
  2. unordered_map的使用方法和map几乎完全一致。记得引入头文件然后声明的时候声明就好啦。所以也不再单独再重新写一篇文章啦。
1
2
3
4
// 引入头文件
#include <unordered_map>
// 使用的时候如同下面这种方式声明即可。
unordered_map<int,int> mmap;

同样的需要记住的是,带有unordered前缀的容器都是没有upper_bound和lower_bound的。

multimap的测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
#include <stdio.h>
#include <iostream>
#include <map>
#include <typeinfo>
using namespace std;

template<class K>
void print(const string s,const K& mmap){
cout<<s<<endl;
for(auto& a:mmap){
cout<<"{"<<a.first<<","<<a.second<<"} ";
}
cout<<endl;
}

int main(){
multimap<int,char> dict {
{1,'A'},
{2,'B'},
{2,'C'},
{3,'D'},
{3,'E'},
{2,'F'}
};
//通过empty()来判断multimap是否为空
cout<<"dict.empty() "<<dict.empty()<<endl;

//通过size()来判断元素个数
cout<<"dict.size() "<<dict.size()<<endl;

print("插入前 size = "+to_string(dict.size()),dict);
// 通过insert插入元素
dict.insert({4,'G'});
dict.insert(pair<int, char>(4,'F'));
dict.insert(make_pair(4, 'H'));
dict.insert({{5,'I'},{5,'J'}});
print("插入后 size = "+to_string(dict.size()),dict);

// 通过erase来删除特定key的元素,参数可以是key,也可以是一个iterator
dict.erase(1);
print("删除key=1后 size = "+to_string(dict.size()),dict);

// 返回类型是pair<iterator,iterator>,仅仅返回符合条件的pair
auto range = dict.equal_range(2);
//cout<<typeid(range).name()<<endl; //pair类型
cout<<"equal_range(2) "<<endl;
for(auto i = range.first;i!=range.second;++i){
cout<<"i.first\t"<<i->first<<"\ti.second\t"<<i->second<<endl;
}

// lower_bound
auto lower = dict.lower_bound(2);
cout<<"lower_bound(2) "<<(lower->first)<<" "<<lower->second<<endl;

// upper_bound
auto upper = dict.upper_bound(2);
cout<<"upper_bound(2) "<<(upper->first)<<" "<<upper->second<<endl;

// equal_range返回的两个pair实际上就是用lower_bound和upper_bound来实现的
cout<<"lower_bound == range.first "<< (lower == range.first)<<endl;
cout<<"upper_bound == range.second "<< (upper == range.second)<<endl;

//通过count来判断key等于给定的值一共有多少个pair
cout<<"count(2) "<<dict.count(2)<<endl;
//这个数量可以通过range的两个iterator来实现查找
cout<<"distance(range.first,range.second) "<<distance(range.first,range.second)<<endl;
//或者使用lower_bound和upper_bound来实现
cout<<"distance(lower,upper)"<<distance(lower, upper)<<endl;

//通过find来判断是否存在key,返回一个iterator,如果有多个key符合条件,不一定会返回哪一个。
auto find2 = dict.find(2);
cout<<"find2 "<<find2->first<<" "<<find2->second<<endl;

//如果通过find来查找的时候没有找到对应的key,那么就会返回dict.end()这个iterator
auto find10 = dict.find(10);
cout<<"find10==dict.end "<<(find10==dict.end())<<endl;

auto a = dict.begin();
cout<<"dict.begin().type "<<typeid(a).name()<<endl;

//关于各类iterator
cout<<"正向遍历dict.begin()->dict.end()"<<endl;
for(auto a = dict.begin(); a!=dict.end(); a++){
// a->second = 'M'; 这里修改就没有问题
cout<<"{"<<a->first<<","<<a->second<<"} ";
}

cout<<"正向遍历,dict.cbegin()->dict.cend()"<<endl;
for(auto a = dict.cbegin(); a!=dict.cend(); a++){
// a -> second = 'N'; 这里修改就有问题,原因是返回的const_iterator是不允许修改的。
cout<<"{"<<a->first<<","<<a->second<<"} ";
}
cout<<endl;

cout<<"反向遍历,dict.rbegin()->dict.rend()"<<endl;
for(auto a = dict.rbegin(); a!=dict.rend(); a++){
cout<<"{"<<a->first<<","<<a->second<<"} ";
}
cout<<endl;

cout<<"反向遍历,dict.rbegin()->dict.rend()"<<endl;
for(auto a = dict.rbegin(); a!=dict.rend(); a++){
cout<<"{"<<a->first<<","<<a->second<<"} ";
}
cout<<endl;

cout<<"反向遍历,dict.crbegin()->dict.crend()"<<endl;
for(auto a = dict.crbegin(); a!=dict.crend(); a++){
cout<<"{"<<a->first<<","<<a->second<<"} ";
}
cout<<endl;

return 0;
}

/* 运行结果
dict.empty() 0
dict.size() 6
插入前 size = 6
{1,A} {2,B} {2,C} {2,F} {3,D} {3,E}
插入后 size = 11
{1,A} {2,B} {2,C} {2,F} {3,D} {3,E} {4,G} {4,F} {4,H} {5,I} {5,J}
删除key=1后 size = 10
{2,B} {2,C} {2,F} {3,D} {3,E} {4,G} {4,F} {4,H} {5,I} {5,J}
equal_range(2)
i.first 2 i.second B
i.first 2 i.second C
i.first 2 i.second F
lower_bound(2) 2 B
upper_bound(2) 3 D
lower_bound == range.first 1
upper_bound == range.second 1
count(2) 3
distance(range.first,range.second) 3
distance(lower,upper)3
find2 2 B
find10==dict.end 1
dict.begin().type NSt3__114__map_iteratorINS_15__tree_iteratorINS_12__value_typeIicEEPNS_11__tree_nodeIS3_PvEElEEEE
正向遍历dict.begin()->dict.end()
{2,B} {2,C} {2,F} {3,D} {3,E} {4,G} {4,F} {4,H} {5,I} {5,J} 正向遍历,dict.cbegin()->dict.cend()
{2,B} {2,C} {2,F} {3,D} {3,E} {4,G} {4,F} {4,H} {5,I} {5,J}
反向遍历,dict.rbegin()->dict.rend()
{5,J} {5,I} {4,H} {4,F} {4,G} {3,E} {3,D} {2,F} {2,C} {2,B}
反向遍历,dict.rbegin()->dict.rend()
{5,J} {5,I} {4,H} {4,F} {4,G} {3,E} {3,D} {2,F} {2,C} {2,B}
反向遍历,dict.crbegin()->dict.crend()
{5,J} {5,I} {4,H} {4,F} {4,G} {3,E} {3,D} {2,F} {2,C} {2,B}
Program ended with exit code: 0
*/