在看这篇文章之前,先看看#include <map>的简介。
#include <map>https://blog.youkuaiyun.com/NOIP1ding_c/article/details/144293214这篇文章我来给大家一个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;
}