C++ 中的map容器详解
什么是map容器?
Map是C++ STL(标准模板库)中提供的一种关联容器,它存储的是键值对(key-value pairs)元素,其中每个键都是唯一的。Map容器内部实现是一棵红黑树(平衡二叉搜索树),这使得map中的元素按照键(key)自动排序。
map容器的主要特点:
- 键值对存储 - 每个元素包含一个键和一个相关联的值
- 唯一键 - 每个键在map中只能出现一次
- 自动排序 - 元素按照键的顺序被自动排序
- 快速访问 - 可以通过键快速查找对应的值
提示
如果你需要一个可以通过键快速查找值的数据结构,而且希望这些元素能够保持顺序,map是一个很好的选择!
map容器的基本语法
头文件和声明
要使用map容器,需要先包含相应的头文件:
#include <map>
int main() {
// 创建一个将字符串映射到整数的map
std::map<std::string, int> studentScores;
// 其他代码...
return 0;
}
插入元素
向map中插入元素有多种方法:
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> studentScores;
// 方法1:使用insert()函数插入pair
studentScores.insert(std::pair<std::string, int>("Alice", 95));
// 方法2:使用make_pair()函数
studentScores.insert(std::make_pair("Bob", 89));
// 方法3:使用数组下标操作符
studentScores["Charlie"] = 78;
studentScores["David"] = 92;
// 输出map中的元素
for (const auto& student : studentScores) {
std::cout << student.first << ": " << student.second << std::endl;
}
return 0;
}
输出结果:
Alice: 95
Bob: 89
Charlie: 78
David: 92
备注
注意到输出的元素按照键(学生姓名)的字母顺序排列,这是因为map默认使用<
运算符对键进行排序。
map容器的常用操作
查找元素
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> studentScores;
studentScores["Alice"] = 95;
studentScores["Bob"] = 89;
studentScores["Charlie"] = 78;
// 使用find()函数查找元素
auto it = studentScores.find("Bob");
if (it != studentScores.end()) {
std::cout << "找到了Bob的分数:" << it->second << std::endl;
} else {
std::cout << "没有找到Bob" << std::endl;
}
// 使用count()函数检查键是否存在
if (studentScores.count("David") > 0) {
std::cout << "David的分数是:" << studentScores["David"] << std::endl;
} else {
std::cout << "没有David的记录" << std::endl;
}
return 0;
}
输出结果:
找到了Bob的分数:89
没有David的记录
删除元素
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> studentScores;
studentScores["Alice"] = 95;
studentScores["Bob"] = 89;
studentScores["Charlie"] = 78;
studentScores["David"] = 92;
std::cout << "初始map大小: " << studentScores.size() << std::endl;
// 通过键删除元素
studentScores.erase("Bob");
std::cout << "删除Bob后map大小: " << studentScores.size() << std::endl;
// 通过迭代器删除元素
auto it = studentScores.find("Charlie");
if (it != studentScores.end()) {
studentScores.erase(it);
}
std::cout << "删除Charlie后map大小: " << studentScores.size() << std::endl;
// 删除所有元素
studentScores.clear();
std::cout << "清空后map大小: " << studentScores.size() << std::endl;
return 0;
}
输出结果:
初始map大小: 4
删除Bob后map大小: 3
删除Charlie后map大小: 2
清空后map大小: 0
遍历map
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> studentScores;
studentScores["Alice"] = 95;
studentScores["Bob"] = 89;
studentScores["Charlie"] = 78;
// 方法1:使用范围for循环(C++11及以上)
std::cout << "使用范围for循环遍历:" << std::endl;
for (const auto& pair : studentScores) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
// 方法2:使用迭代器
std::cout << "\n使用迭代器遍历:" << std::endl;
for (auto it = studentScores.begin(); it != studentScores.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
return 0;
}
输出结果:
使用范围for循环遍历:
Alice: 95
Bob: 89
Charlie: 78
使用迭代器遍历:
Alice: 95
Bob: 89
Charlie: 78
map的其他常用功能
检查容器是否为空
if (studentScores.empty()) {
std::cout << "容器为空" << std::endl;
} else {
std::cout << "容器不为空,包含 " << studentScores.size() << " 个元素" << std::endl;
}