C++ 迭代HashMap C++;

C++ 迭代HashMap C++;,c++,hashmap,C++,Hashmap,如何迭代hashMap(C++),以便遍历所有元素 我有一个名为map的散列: HashMap<std::string, int> map; map.insert (key1, value1); map.insert (key2, value2); map.insert (key3, value3); ... ... ... map.insert (keyN, valueN); HashMap; map.insert(键1,值1); map.inser

如何迭代hashMap(C++),以便遍历所有元素

我有一个名为map的散列:

HashMap<std::string, int> map;

map.insert (key1, value1);
map.insert (key2, value2);
map.insert (key3, value3);

    ...
    ...
    ...

map.insert (keyN, valueN);
HashMap;
map.insert(键1,值1);
map.insert(键2,值2);
地图插入(键3,值3);
...
...
...
地图插入(键,值);

哈希映射
已被弃用,但同样的原则适用于。。。您可以使用范围循环(在这种情况下,使用的是无序的\u映射

使用“自动”将使其类似于:

#include <iostream>
#include <string> 
#include <unordered_map>
int main()
{
    std::unordered_map<std::string, int> myMap;

    myMap.insert(std::pair<std::string, int>("a", 1));
    myMap.insert(std::pair<std::string, int>("b", 2));
    myMap.insert(std::pair<std::string, int>("c", 3));
    myMap.insert(std::pair<std::string, int>("d", 4));
    for (const auto& x : myMap)
    {
        std::cout << "fisrt: " << x.first << ", second: " << x.second << std::endl;
    }
    std::cin.get();
    return 0;
}
#包括
#包括
#包括
int main()
{
std::无序图myMap;
插入(std::pair(“a”,1));
插入(std::pair(“b”,2));
插入(std::pair(“c”,3));
插入(std::pair(“d”,4));
用于(const auto&x:myMap)
{
std::cout如果它同时具有begin()和end()成员函数,那么您应该能够简单地将其放入范围循环中

for (auto& entry : map)
    //whatever you want here

你尝试了什么?它不应该比任何其他容器都不同。可能的重复< <代码> HashMap <代码>不是C++的一部分。如果你使用第三方库,你需要研究它的文档。