在C++中,可以使用迭代器来遍历哈希表。以下是一个示例代码:
#include#include int main() { std::unordered_map myMap = { {1, "One"}, {2, "Two"}, {3, "Three"} }; // 遍历哈希表 for (auto it = myMap.begin(); it != myMap.end(); ++it) { std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl; } return 0; }
在上面的示例中,我们使用unordered_map
来创建一个哈希表,并使用迭代器it
来遍历哈希表。我们可以通过it->first
和it->second
来访问哈希表中的键和值。