#include <iostream>#include <string>#include <map>class Data{ public: Data(); void SetData(); std::map< std::string, std::map< std::string, std::string > >& GetData(){ return data; }; std::map< std::string, std::string > GetData(std::string key){ return data[key]; }; //end public: private: std::map< std::string, std::map< std::string, std::string > > data; int countKey; //end private:};Data::Data(){ countKey=0;}void Data::SetData(){ std::map <std::string, std::string> tempdata; tempdata.insert(std::pair<std::string, std::string >( "1" , "one" )); tempdata.insert(std::pair<std::string, std::string >( "2" , "two" )); tempdata.insert(std::pair<std::string, std::string >( "3" , "three" )); tempdata.insert(std::pair<std::string, std::string >( "4" , "four" )); std::string key; if ( countKey == 0 ) key="USA"; if ( countKey == 1 ) key="UK"; if ( countKey == 2 ) key="JAPAN"; if ( countKey == 3 ) key="GERMANY"; if ( countKey == 4 ) key="RUSSIA"; if ( countKey == 5 ) key="INDIA"; data.insert(std::pair<std::string, std::map<std::string, std::string> >( key , tempdata )); countKey++;}int main( int argc, char* argv[] ){ //make an object Data. //Data objects store map<string,map> type of data. Data mainData; int countData=0; //make 4 Set Datas. //meaning, make up to RUSSIA. for( int i=5; i > 0; i-- ){ mainData.SetData( ); countData++; } //countData should be 4, so dynamically create an array of iterators that can hold 4 iterators. std::map< std::string, std::map< std::string, std::string > >::iterator mainlooparray[countData]; //reuse countData to assign the correct array# to mainlooparray. countData=0; for( std::map< std::string, std::map< std::string, std::string > >::iterator mainloop=mainData.GetData().begin(); mainloop != mainData.GetData().end(); ++mainloop ){ mainlooparray[countData]=mainloop; countData++; } //again, countData should be 4 //more over mainlooparray should be an iterator of specific positions of data. //mainloop is declared, and mainloop's main purpose is to hold what is inside mainlooparray[specific_iterator] std::map< std::string, std::map< std::string, std::string > >::iterator mainloop; //mainloop2's job is to iterate a map. std::map< std::string, std::string >::iterator mainloop2; //error is caused here. for ( int i=0; i<countData; i++ ){ mainloop=mainlooparray; for ( mainloop2=mainloop->second.begin(); mainloop2!=mainloop->second.end(); ++mainloop2 ){ std::cout << mainloop2->first << "=" << mainloop2->second; } } return 0;}
gdb errors:
#0 0x0804a82c in std::_Rb_tree<std::string, std::pair<std::string const, std::string>, std::_Select1st<std::pair<std::string const, std::string> >, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > >::begin ()
#1 0x08049fb5 in std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > >::begin ()
#2 0x08049838 in main ()
The above is a compilable code of the problem.
I can not communicate with words to fix this problem, if somebody can demonstrate me with code, that will be very helpful. I'm NOT trying to make you guys work for me, it took me 30 minutes just to make the code to a bare minimum where it still produces errors.
Thanks.