以下详细介绍 C++ std::map 的核心用法,结合基本操作、进阶技巧和典型场景,帮助你高效使用这一关联容器:
📦 一、基础操作
-
定义与初始化
- 需包含头文件:
#include <map> - 声明示例:
std::map<int, std::string> studentScores; // 键为学号(int),值为姓名(string)
- 需包含头文件:
-
插入元素
- **
insert方法**:studentScores.insert(std::make_pair(1001, "Alice")); studentScores.insert({1002, "Bob"}); // C++11 简化语法 - **
operator[]方法**(若键不存在则自动创建):studentScores[1003] = "Charlie"; // 直接赋值插入
- **
-
访问元素
- **
operator[]**:std::string name = studentScores[1001]; // 若键不存在,会创建默认值(空字符串) - **
find方法**(安全访问):auto it = studentScores.find(1002); if (it != studentScores.end()) { std::cout << "Found: " << it->second; // it->second 获取值 }
- **
⚙️ 二、进阶操作
-
删除元素
- **
erase方法**:studentScores.erase(1001); // 按键删除 studentScores.erase(it); // 按迭代器删除 - 安全删除建议:遍历时需先收集待删键,再统一删除,避免迭代器失效。
- **
-
遍历元素
- 迭代器遍历(按键升序):
for (auto it = studentScores.begin(); it != studentScores.end(); ++it) { std::cout << it->first << ": " << it->second << std::endl; // first为键,second为值 } - 范围循环(C++11):
for (const auto& pair : studentScores) { std::cout << pair.first << ": " << pair.second << std::endl; }
- 迭代器遍历(按键升序):
-
检查键是否存在
- 使用
find或count:if (studentScores.find(1003) != studentScores.end()) { /* 存在 */ } if (studentScores.count(1003) > 0) { /* 存在(返回1或0)*/ }
- 使用
🧩 三、特性与自定义
-
自动排序
map基于红黑树实现,按键升序排列(默认std::less<Key>)。- 示例:插入
{3, "C"}, {1, "A"}, {2, "B"},遍历输出顺序为1:A, 2:B, 3:C。
-
自定义排序规则
- 通过函数对象定义降序:
struct CompareDesc { bool operator()(int a, int b) const { return a > b; } }; std::map<int, std::string, CompareDesc> descMap; // 按键降序
- 通过函数对象定义降序:
🚀 四、典型应用场景
- 统计频率
std::map<std::string, int> wordCount; for (const auto& word : words) { wordCount[word]++; // 自动初始化未出现的键为0 } - 键值映射存储
如存储学生 ID 到成绩的映射:std::map<int, double> scores = {{101, 95.5}, {102, 88.0}}; double score = scores.at(101); // at() 方法在键不存在时抛出异常(更安全)
⚠️ 注意事项
- 键的唯一性:重复插入同一键会覆盖旧值。
- **
operator[]的风险**:访问不存在的键会创建默认值,可能意外增加元素。建议用find()或count()检查。 - 性能:插入、删除、查找操作时间复杂度为 O(log n),适合需有序键的场景。若无需排序,可用
std::unordered_map(哈希表实现,O(1) 操作)。
通过灵活应用上述方法,可高效管理键值对数据,满足各类有序映射需求。完整代码示例可参考来源文档。
2413

被折叠的 条评论
为什么被折叠?



