#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> myMap;
// 1. 向 myMap 中插入至少三个键值对
myMap.insert({1, "one"});
myMap.insert({2, "two"});
myMap.insert({3, "three"});
// 2. 通过键来检索对应的值并输出
int keyToSearch = 2;
auto it = myMap.find(keyToSearch);
if (it != myMap.end()) {
std::cout << "The value for key " << keyToSearch << " is: " << it->second << std::endl;
} else {
std::cout << "Key " << keyToSearch << " not found." << std::endl;
}
// 3. 遍历 myMap 并输出所有的键值对
std::cout << "All key - value pairs in myMap:" << std::endl;
for (const auto& pair : myMap) {
std::cout << pair.first << " : " << pair.second << std::endl;
}
return 0;
}