C++ Loop through Map

前端 未结 5 701
抹茶落季
抹茶落季 2020-11-30 16:56

I want to iterate through each element in the map without knowing any of its string-int values or keys.

What I have so far:



        
5条回答
  •  我在风中等你
    2020-11-30 17:30

    The value_type of a map is a pair containing the key and value as it's first and second member, respectively.

    map::iterator it;
    for (it = symbolTable.begin(); it != symbolTable.end(); it++)
    {
        std::cout << it->first << ' ' << it->second << '\n';
    }
    

    Or with C++11, using range-based for:

    for (auto const& p : symbolTable)
    {
        std::cout << p.first << ' ' << p.second << '\n';
    }
    

提交回复
热议问题