在 C++ 中,<map> 是标准模板库(STL)的一部分,它提供了一种关联容器,用于存储键值对(key-value pairs)。
map 容器中的元素是按照键的顺序自动排序的,这使得它非常适合需要快速查找和有序数据的场景。
定义和特性
基本语法
包含头文件:
#include <map>
声明 map 容器:
std::map<key_type, value_type> myMap;
插入元素:
myMap[key] = value;
访问元素:
value = myMap[key];
遍历 map:
for (std::map<key_type, value_type>::iterator it = myMap.begin(); it != myMap.end(); ++it) { std::cout << it->first << " => " << it->second << std::endl; }
C++11 及以上标准,遍历部分可以简化为范围 for 循环,代码更简洁:
for (auto &p : m) { std::cout << p.first << " : " << p.second << std::endl; }
实例
下面是一个使用 map 的简单实例,我们将创建一个 map 来存储员工的姓名和他们的年龄,并遍历这个 map 来打印每个员工的姓名和年龄。
#include <iostream>#include <map>#include <string> int main() { // 创建一个 map 容器,存储员工的姓名和年龄 std::map<std::string, int> employees; // 插入员工信息 employees["Alice"] = 30; employees["Bob"] = 25; employees["Charlie"] = 35; // 遍历 map 并打印员工信息 for (std::map<std::string, int>::iterator it = employees.begin(); it != employees.end(); ++it) { std::cout << it->first << " is " << it->second << " years old." << std::endl; } return 0;}
输出结果:
Alice is 30 years old. Bob is 25 years old. Charlie is 35 years old.
进阶用法
检查键是否存在:
if (myMap.find(key) != myMap.end()) { // 键存在 }
删除元素:
myMap.erase(key);
清空 map:
myMap.clear();
获取 map 的大小:
size_t size = myMap.size();
其他方法:
myMap.empty(); // 是否为空 myMap.count("Bob"); // key 是否存在(返回 0 或 1)
自定义排序,默认升序排序,可以用 std::greater 或自定义比较函数:
std::map<int, std::string, std::greater<int>> m; // 降序
使用自定义比较函数:
#include <map>#include <string>#include <functional> bool myCompare(const std::string& a, const std::string& b) { return a < b;} int main() { std::map<std::string, int, std::function<bool(const std::string&, const std::string&)>> myMap(myCompare); // 其他操作... return 0;}
map 是 C++ STL 中一个非常有用的容器,特别适合需要快速查找和有序数据的场景。
#include <iostream>#include <map>#include <string> int main() { std::map<std::string, int> scores; // 插入 scores["Alice"] = 90; scores["Bob"] = 85; scores.insert({"Charlie", 92}); // 遍历 for (auto &p : scores) { std::cout << p.first << " => " << p.second << std::endl; } // 查找 auto it = scores.find("Bob"); if (it != scores.end()) { std::cout << "Bob's score: " << it->second << std::endl; } // 删除 scores.erase("Alice"); std::cout << "Size: " << scores.size() << std::endl; return 0;}
运行结果(自动按 key 排序):
Alice => 90 Bob => 85 Charlie => 92 Bob's score: 85 Size: 2