简体   繁体   中英

iterate over map within map

I have a problem iterating over the second map in a map.

#include <map>

using namespace std;

map<string, map<string, string> > mymap;
map<string, map<string, string> >::iterator itm;
pair<map<string, map<string, string> >::iterator,bool> retm;

for( itm=mymap.begin(); itm!=mymap.end(); ++itm)
{
cout << "first:\t" << it->first << endl;
}

How can I iterate over the second map and get both first and second keys/values?

And the second question is, how can I "insert" into the first and second map using the "insert" function that comes with maps?

I hope someone has a full answer.

it->second will give you the "second map". Just iterate over that.

#include <map>
#include <iostream>
using namespace std;

map<string, map<string, string> > mymap;
map<string, map<string, string> >::iterator itm;
pair<map<string, map<string, string> >::iterator,bool> retm;

int main() {

  /* populate: */
  mymap.insert(make_pair ("something", map<string, string>()));
  mymap["something"].insert(make_pair ("2", "two"));

  /* traverse: */
  for( itm=mymap.begin(); itm!=mymap.end(); ++itm)
  {
    cout << "first:\t" << itm->first << endl;

    for (map<string, string>::iterator inner_it = (*itm).second.begin();
        inner_it != (*itm).second.end(); 
        inner_it++) {
      cout << (*inner_it).first << " => " << (*inner_it).second << endl;
    }   

  }

  return 0;
}

就像在mymap上进行迭代一样,您将需要在另一个嵌套的for循环中使用第二个迭代器在it-> second上进行迭代。

Like this:

typedef std::map<std::string, std::map<std::string, std::string>>::iterator outer_it;
typedef std::map<std::string, std::string>::iterator                        inner_it;

for (outer_it it1 = mymap.begin(); it1 != mymap.end(); ++it1)
{
    for (inner_it it2 = it1->second.begin(); it2 != it1->second.end(); ++it2)
    {
        std::cout << "first: " << it1->first << ", second: " << it2->first
                  << ", value: " << it2->second << std::endl;
    }
}

To insert:

mymap["hello"]["world"] = "foo";

Or, using insert :

mymap["hello"].insert(std::make_pair("world", "foo"));

If you want to insert multiple values, perform the outer lookup only once:

std::map<std::string, std::string> & m = mymap["hello"];
m.insert(std::make_pair("world", "foo"));
m.insert(std::make_pair("word",  "boo"));
m.insert(std::make_pair("lord",  "coo"));

In C++11, you can do it like this:

for (const auto& outerElem: mymap) {
    cout << "First: " << outerElem.first << endl;
    for (const auto& innerElem: outerElem.second) {
        cout << "  First: " << innerElem.first << endl;
        cout << "  Second: " << innerElem.second << endl;
    }
}

In C++03, you can also do this with BOOST_FOREACH . You cannot use auto though, so it's best to typedef each type like this. Using typedefs like this is a good idea anyway.

typedef map<string, string> innerMap;
typedef map<string, innerMap> outerMap;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM