c++ - How to print all elements in a 2D map given the 1st index -


as title says want print out elements of "group1" in 2d map. tried huge error @ loop.

#include<iostream> #include<string> #include<map> using namespace std;  int main(){   map<string,map<int,double> > mymap;   mymap["group1"][13] = 10.41;   mymap["group1"][15] = 31.2;    //print elements in "group1"   (map< string, map<int,double> >::const_iterator iter =         mymap["group1"].begin(); iter != mymap["group1"].end(); ++iter)        {          cout << iter->first << '\t' << iter->second << '\n';        }    return 0; }  

any appreciated, thanks!

the type of mymap["group1"] map<int,double> , not map<string,map<int,double>>

then code somehow this:

const map<int,double> &grp1map = mymap["group1"]; (map<int,double>::const_iterator iter = grp1map.begin(); iter != grp1map.end(); ++iter) {     cout << iter->first << '\t' << iter->second << '\n'; } 

you can simplify auto:

auto &grp1map = mymap["group1"]; (auto iter = grp1map.begin(); iter != grp1map.end(); ++iter)  {     cout << iter->first << '\t' << iter->second << '\n'; } 

but anyway should this:

for (auto &elem : mymap["group1"] ) {     cout << elem.first << '\t' << elem.second << '\n'; } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -