目录
- 一、map 简介
- 二、map 构造函数
- 三、map 赋值操作
- 四、map 数据插入与删除
- 五、map 查找与统计
- 六、map 元素数量与交换
- 七、map 迭代器
- 八、map 容器排序
- 九、map 与 multimap 区别
- 如果这篇文章对你有所帮助,渴望获得你的一个点赞!
一、map 简介
C++ 中,std::map
和multimap
都属于关联式容器,且都包含在头文件#include <map>
中,其底层结构都是用二叉树实现的。
-
map
中所有元素都是pair
,pair
中第一个元素为键值(key
),起索引的作用;第二个元素为实值(value
),即映射值 - 该类容器中元素在插入时都会根据元素的键值(
key
)自动排序
二、map 构造函数
构造函数原型 | 解释 | |
1 | map<T1, T2> mp | 默认构造 |
2 | map(const map &mp) | 拷贝构造 |
三、map 赋值操作
函数原型:= | 解释 | |
1 | set& operator=(const set &st) | 重载=操作符 |
四、map 数据插入与删除
函数原型:insert、erase、clear | 解释 | |
1 | insert(elem) | 在容器中插入元素 |
2 | erase(key) | 删除容器中键值为key的元素 |
3 | erase(pos) | 删除pos迭代器所指的元素,返回下一个元素的迭代器 |
4 | erase(beg, end) | 删除区间[beg,end)的所有元素,返回下一个元素的迭代器 |
5 | clear() | 清除所有元素 |
示例1:map<string, int>
#include <iostream>
#include <string>
#include <map> //必须包含该头文件
using namespace std;
void printmap(map<string,int>& s)
{
for (map<string,int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << "键值:" << it->first << "\t实值:" << it->second << endl;
}
cout << endl;
}
void test01()
{
map<string,int> s1;
//第1种插入方式
s1["娃哈哈"] = 2;
//第2种插入方式
s1.insert(pair<string,int>("可乐", 3));
//第3种插入方式
s1.insert(make_pair("脉动", 4));
//第4种插入方式
s1.insert(map<string, int>::value_type("红牛", 5));
cout << "插入后的元素:" << endl;
printmap(s1); //string类型key会根据首字母顺序
s1.erase("娃哈哈");
cout << "删除娃哈哈后的元素:" << endl;
printmap(s1);
s1.clear();
cout << "清空后的元素:" << endl;
printmap(s1);
}
int main()
{
test01();
system("pause");
return 0;
}
//result
插入后的元素:
键值:红牛 实值:5
键值:可乐 实值:3
键值:脉动 实值:4
键值:娃哈哈 实值:2
删除娃哈哈后的元素:
键值:红牛 实值:5
键值:可乐 实值:3
键值:脉动 实值:4
清空后的元素:
注意:
-
map
容器插入后的数据会根据key
自动排序,string
类型的key
会根据首字母进行排序 -
map
容器插入重复key值的元素会失败,元素数量不会发生改变
示例2:map<int, double>
#include <iostream>
#include <string>
#include <map> //必须包含该头文件
using namespace std;
void printmap(map<int, double>& s)
{
for (map<int, double>::iterator it = s.begin(); it != s.end(); it++)
{
cout << "键值:" << it->first << "\t实值:" << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, double> s1;
//第1种插入方式
s1[1] = 1.001;
//第2种插入方式
s1.insert(pair<int, double>(4, 1.004));
//第3种插入方式
s1.insert(make_pair(2, 1.002));
//第4种插入方式
s1.insert(map<int, double>::value_type(3, 1.003));
cout << "插入后的元素:" << endl;
printmap(s1); //string类型key会根据首字母顺序
s1.erase(1);
cout << "删除key=1的元素:" << endl;
printmap(s1);
s1.clear();
cout << "清空后的元素:" << endl;
printmap(s1);
}
int main()
{
test01();
system("pause");
return 0;
}
//result
插入后的元素:
键值:1 实值:1.001
键值:2 实值:1.002
键值:3 实值:1.003
键值:4 实值:1.004
删除key=1的元素:
键值:2 实值:1.002
键值:3 实值:1.003
键值:4 实值:1.004
清空后的元素:
五、map 查找与统计
函数原型:find、count | 解释 | |
1 | find(key) | 查找key是否存在, 若存在则返回该键值元素的迭代器;若不存在则返回map.end() |
2 | count(key) | 统计key的元素个数(对于map,结果为0或者1) |
注意:
-
find(key)
返回的是迭代器,可以通过iterator->first
和iterator->second
得到key
和value
- 因为
map
中元素不重复,所以count()
返回0或者1
示例:
#include <iostream>
#include <string>
#include <map> //必须包含该头文件
using namespace std;
void test01()
{
map<string, int> s1;
//插入insert
s1["娃哈哈"] = 2;
s1.insert(pair<string, int>("可乐", 3));
s1.insert(make_pair("脉动", 4));
s1.insert(map<string, int>::value_type("红牛", 5));
//查找find
map<string, int>::iterator pos = s1.find("脉动");
if (pos != s1.end())
{
cout << "找到元素: key = " << pos->first << " value = " << pos->second << endl;
}
else
{
cout << "未到元素key!";
}
//统计count
int num1 = s1.count("可乐");
cout << "可乐 num = " << num1 << endl;
int num2 = s1.count("雪碧");
cout << "雪碧 num = " << num2 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
找到元素: key = 脉动 value = 4
可乐 num = 1
雪碧 num = 0
六、map 元素数量与交换
函数原型:empty、size、swap | 解释 | |
1 | empty() | 判断容器是否为空 |
2 | size() | 返回容器中元素的数量 |
3 | swap(set<T> & st) | 交换两个容器中的元素 |
注意:swap()
交换的两个容器中包含元素的类型必须相同。
七、map 迭代器
函数原型:begin、end | 解释 | |
1 | begin() | 返回一个迭代器,指向set容器的首元素 |
2 | end() | 返回一个迭代器,指向set容器的末尾元素的下一个位置 |
3 | rbegin() | 返回一个反向迭代器,指向set容器的最后一个元素 |
4 | rend() | 返回一个反向迭代器,指向set容器的首元素的上一个位置 |
5 | cbegin() | 返回一个const迭代器,指向set容器的首元素 |
6 | cend() | 返回一个const迭代器,指向set容器的末尾元素的下一个位置 |
7 | crbegin() | 返回一个const反向迭代器,指向set容器的末尾元素 |
8 | crend() | 返回一个const反向迭代器,指向set容器的首的上一个位置 |
注意:const iterator
是一个指向const
值的迭代器。迭代器本身可以修改,但是它不能被用来修改它所指向的值。
八、map 容器排序
- map容器默认排序规则为
key
值从小到大 - 利用仿函数,可以改变排序规则
- 对于自定义数据类型,
map
必须指定key
值排序规则,同set
【set使用方法】
示例:
#include <iostream>
#include <string>
#include <map> //必须包含该头文件
using namespace std;
//仿函数,重载operator()运算符
class Compare
{
public:
//Visual Studio的编译器此处需添加const,具有指定const-volatile类型的变量只能调用具有相同或更大const-volatile限定定义的成员函数
bool operator()(int v1, int v2)const
{
return v1 > v2;
}
};
void test01()
{
map<int,string,Compare> s1; //指定排序规则, 从大到小
s1.insert(pair<int, string>(1, "李四"));
s1.insert(pair<int, string>(3, "张三"));
s1.insert(pair<int, string>(4, "王五"));
s1.insert(pair<int, string>(2, "赵六"));
for (map<int, string>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << "键值:" << it->first << "\t实值:" << it->second << endl;
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
键值:4 实值:王五
键值:3 实值:张三
键值:2 实值:赵六
键值:1 实值:李四
九、map 与 multimap 区别
-
map
不允许容器中有重复的元素 -
multimap
允许容器中有重复的元素 -
map
插入数据的同时会返回插入结果,表示插入是否成功 -
multimap
不会检测数据,因此可以插入重复数据
示例:multimap
的find
方法
#include <iostream>
#include <string>
#include <map> //必须包含该头文件
using namespace std;
void test01()
{
multimap<int, string> s1;
s1.insert(pair<int, string>(1, "李四"));
s1.insert(pair<int, string>(3, "张三"));
s1.insert(pair<int, string>(1, "丁一"));
s1.insert(pair<int, string>(4, "王五"));
s1.insert(pair<int, string>(2, "赵六"));
s1.insert(pair<int, string>(1, "吴二"));
int sum = s1.count(1);
cout << "key = 1的元素数量:" << sum << endl;
//因为map会自动排列,key相同的元素必定连续排列,所以从第一个find(1)位置遍历count(1)次
multimap<int, string>::iterator it = s1.find(1);
for (int i = 0; i != s1.count(1); i++,it++)
{
cout << "键值:" << it->first << "\t实值:" << it->second << endl;
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
key = 1的元素数量:3
键值:1 实值:李四
键值:1 实值:丁一
键值:1 实值:吴二
注意: 因为multimap
会自动排列,key
相同的元素必定连续排列,所以从第一个find(1)
位置遍历count(1)
次,即可获得所有key = 1
的键值对元素。