在C++中,可以使用以下方法來遍歷unordered_map:
std::unordered_map<int, std::string> myMap = {{1, "apple"}, {2, "banana"}, {3, "orange"}};
for(auto it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;
}
std::unordered_map<int, std::string> myMap = {{1, "apple"}, {2, "banana"}, {3, "orange"}};
for(const auto& pair : myMap) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
}
std::unordered_map<int, std::string> myMap = {{1, "apple"}, {2, "banana"}, {3, "orange"}};
std::for_each(myMap.begin(), myMap.end(), [](const std::pair<int, std::string>& pair) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
});
std::unordered_map<int, std::string> myMap = {{1, "apple"}, {2, "banana"}, {3, "orange"}};
for(const auto& pair : myMap) {
std::cout << "Key: " << pair.first << std::endl;
}
for(const auto& pair : myMap) {
std::cout << "Value: " << pair.second << std::endl;
}