ChatGPT解决这个技术问题 Extra ChatGPT

How can I loop through a C++ map of maps?

How can I loop through a std::map in C++? My map is defined as:

std::map< std::string, std::map<std::string, std::string> >

For example, the above container holds data like this:

m["name1"]["value1"] = "data1";
m["name1"]["value2"] = "data2";
m["name2"]["value1"] = "data1";
m["name2"]["value2"] = "data2";
m["name3"]["value1"] = "data1";
m["name3"]["value2"] = "data2";

How can I loop through this map and access the various values?


R
Riot

Old question but the remaining answers are outdated as of C++11 - you can use a ranged based for loop and simply do:

std::map<std::string, std::map<std::string, std::string>> mymap;

for(auto const &ent1 : mymap) {
  // ent1.first is the first key
  for(auto const &ent2 : ent1.second) {
    // ent2.first is the second key
    // ent2.second is the data
  }
}

this should be much cleaner than the earlier versions, and avoids unnecessary copies.

Some favour replacing the comments with explicit definitions of reference variables (which get optimised away if unused):

for(auto const &ent1 : mymap) {
  auto const &outer_key = ent1.first;
  auto const &inner_map = ent1.second;
  for(auto const &ent2 : inner_map) {
    auto const &inner_key   = ent2.first;
    auto const &inner_value = ent2.second;
  }
}

Props for keeping the answers relevant -- I only wish this could rise its way nearer to the top. Perhaps editing this in to the accepted answer would be appropriate? (It's what we do on TeX.SX, but SO is a different culture.)
Just a quick question, is there any relevance to your decision of writing const after auto? Is it purely aesthetic?
@Parham const before or after a type specified is a matter of preference, but I choose to keep it on the right because it makes it clearer in situations where pointers are being used; for instance when using both int const *x and int *const x you can write it as int const *const x which is much clearer IMO than const int *const x. But it's just parsed from left to right so the effect is the same. See the answers to this question: stackoverflow.com/questions/5503352/const-before-or-const-after
what does the & mean in the auto const &ent2?
@TannerSummers because accessing by value would add the inefficiency of copying each element; additionally if you wanted to modify the contents, you'd need to access the elements by references (or pointers) rather than by value.
P
Puppy

You can use an iterator.

typedef std::map<std::string, std::map<std::string, std::string>>::iterator it_type;
for(it_type iterator = m.begin(); iterator != m.end(); iterator++) {
    // iterator->first = key
    // iterator->second = value
    // Repeat if you also want to iterate through the second map.
}

Unless he intends to modify the map, using const_iterator would be better.
it is more efficient to do ++iterator than iterator++ since it avoids an unnecessary copy when incrementing.
Using auto greatly simplifies the loop for C++11 : for(auto iterator = m.begin(); iterator != m.end(); iterator++)
This is pretty outdated for c++11. Just use for (auto iter : mymap)
For c++11, you should use (auto& iter : mymap) to avoid the potential copy.
A
Axel Gneiting
for(std::map<std::string, std::map<std::string, std::string> >::iterator outer_iter=map.begin(); outer_iter!=map.end(); ++outer_iter) {
    for(std::map<std::string, std::string>::iterator inner_iter=outer_iter->second.begin(); inner_iter!=outer_iter->second.end(); ++inner_iter) {
        std::cout << inner_iter->second << std::endl;
    }
}

or nicer in C++0x:

for(auto outer_iter=map.begin(); outer_iter!=map.end(); ++outer_iter) {
    for(auto inner_iter=outer_iter->second.begin(); inner_iter!=outer_iter->second.end(); ++inner_iter) {
        std::cout << inner_iter->second << std::endl;
    }
}

You should use auto&, or if you don't modify the map, even const auto&. In addition, prefer the non-member begin() and end(), i.e. for(const auto& iter = begin(map); ...).
Or even simpler: for(const auto& element : map) cout << element.second;
e
einpoklum

With C++17 (or later), you can use the "structured bindings" feature, which lets you define multiple variables, with different names, using a single tuple/pair. Example:

for (const auto& [name, description] : planet_descriptions) {
    std::cout << "Planet " << name << ":\n" << description << "\n\n";
}

The original proposal (by luminaries Bjarne Stroustrup, Herb Sutter and Gabriel Dos Reis) is fun to read (and the suggested syntax is more intuitive IMHO); there's also the proposed wording for the standard which is boring to read but is closer to what will actually go in.


This is so pretty I need to vote despite C++17 not being "there" quite yet. Man, they're really revitalizing C++ by making it easier to write clean and safe code.
K
Kevin Reid

Do something like this:

typedef std::map<std::string, std::string> InnerMap;
typedef std::map<std::string, InnerMap> OuterMap;

Outermap mm;

...//set the initial values

for (OuterMap::iterator i = mm.begin(); i != mm.end(); ++i) {
    InnerMap &im = i->second;
    for (InnerMap::iterator ii = im.begin(); ii != im.end(); ++ii) {
        std::cout << "map[" 
                  << i->first 
                  << "][" 
                  << ii->first 
                  << "] =" 
                  << ii->second 
                  << '\n';
    }
}   

In the second for it should be ++ii not ++i :)
I think the '/n' should be a '\n' in the end
Well I would have used defines to undef them later bur this is a good way for C++98 :) +1
u
user1438233

C++11:

std::map< std::string, std::map<std::string, std::string> > m;
m["name1"]["value1"] = "data1";
m["name1"]["value2"] = "data2";
m["name2"]["value1"] = "data1";
m["name2"]["value2"] = "data2";
m["name3"]["value1"] = "data1";
m["name3"]["value2"] = "data2";

for (auto i : m)
    for (auto j : i.second)
        cout << i.first.c_str() << ":" << j.first.c_str() << ":" << j.second.c_str() << endl;

output:

name1:value1:data1
name1:value2:data2
name2:value1:data1
name2:value2:data2
name3:value1:data1
name3:value2:data2

How this answer is different from stackoverflow.com/a/27344958/3658660 ? Except for the fact that it's making copies everywhere.
h
honk

As einpoklum mentioned in their answer, since C++17 you can also use structured binding declarations. I want to extend on that by providing a full solution for iterating over a map of maps in a comfortable way:

int main() {
    std::map<std::string, std::map<std::string, std::string>> m {
        {"name1", {{"value1", "data1"}, {"value2", "data2"}}},
        {"name2", {{"value1", "data1"}, {"value2", "data2"}}},
        {"name3", {{"value1", "data1"}, {"value2", "data2"}}}
    };

    for (const auto& [k1, v1] : m)
        for (const auto& [k2, v2] : v1)
            std::cout << "m[" << k1 << "][" << k2 << "]=" << v2 << std::endl;

    return 0;
}

Output:

m[name1][value1]=data1 m[name1][value2]=data2 m[name2][value1]=data1 m[name2][value2]=data2 m[name3][value1]=data1 m[name3][value2]=data2

Note 1: For filling the map, I used an initializer list (which is a C++11 feature). This can sometimes be handy to keep fixed initializations compact.

Note 2: If you want to modify the map m within the loops, you have to remove the const keywords.

Code on Coliru


A
Amir Saniyan

use std::map< std::string, std::map<std::string, std::string> >::const_iterator when map is const.


You know, it is sometimes not a good habit to hide code behind the right margin. I understand it's safer but well il completely blur the vision of the code. Go auto bro, or he who uses vim will go KO.
A
AmirSalar

First solution is Use range_based for loop, like:

Note: When range_expression’s type is std::map then a range_declaration’s type is std::pair.

for ( range_declaration : range_expression )      
  //loop_statement

Code 1:

typedef std::map<std::string, std::map<std::string, std::string>> StringToStringMap;

StringToStringMap my_map;

for(const auto &pair1 : my_map) 
{
   // Type of pair1 is std::pair<std::string, std::map<std::string, std::string>>
   // pair1.first point to std::string (first key)
   // pair1.second point to std::map<std::string, std::string> (inner map)
   for(const auto &pair2 : pair1.second) 
   {
       // pair2.first is the second(inner) key
       // pair2.second is the value
   }
}

The Second Solution:

Code 2

typedef std::map<std::string, std::string> StringMap;
typedef std::map<std::string, StringMap> StringToStringMap;

StringToStringMap my_map;

for(StringToStringMap::iterator it1 = my_map.begin(); it1 != my_map.end(); it1++)
{
    // it1->first point to first key
    // it2->second point to inner map
    for(StringMap::iterator it2 = it1->second.begin(); it2 != it1->second.end(); it2++)
     {
        // it2->second point to value
        // it2->first point to second(inner) key 
     }
 }