Pointer Type STL Map, How can I Iterate?

Started by
22 comments, last by smart_idiot 18 years ago
#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.
Advertisement
The problem in the above code is that you're looping from 0 to 10 (the value of countData), and there is only 7 elements in the map. You probably want to count from 0 to 7 (the value of countDataTrack).
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Quote:Original post by NotAYakk
You are probably copying the map somewhere.

Copying the map will cause problems -- it is expensive, and it is probably causing your seg faults.


Why would copying maps cause problems?
and how do you know this?
I'm not too familiar with templates so i'm going to look up on templates first before I use the code skippet you have provided me.

Thank you.
Tried to rewrite your sample program.
#include <iostream>#include <string>#include <map>class Data {  public:   typedef std::map<std::string, std::string> InnerMap;   typedef std::map<std::string, InnerMap> OuterMap;      Data();      OuterMap & GetData()    {     return data;    }      InnerMap & GetData(const std::string &key)    {     return data[key];    }    private:   OuterMap data; };Data::Data() {  const char *list[] = {"USA", "UK", "Japan", "Germany", "Russia", "India", 0};    for(const char **name=list; *name; ++name)   {    InnerMap &map = data.insert(OuterMap::value_type(*name, InnerMap())).first->second;        map.insert(InnerMap::value_type("1", "one"));    map.insert(InnerMap::value_type("2", "two"));    map.insert(InnerMap::value_type("3", "three"));    map.insert(InnerMap::value_type("4", "four"));   }}int main() {  Data mainData;    Data::OuterMap &map = mainData.GetData();    for(Data::OuterMap::iterator i = map.begin(); i != map.end(); ++i)   {    std::cout << i->first << ':' << std::endl;        for(Data::InnerMap::iterator j = i->second.begin(); j != i->second.end(); ++j)     std::cout << " >  " << j->first << "=" << j->second << std::endl;   }    return 0; }

You probably won't care much for my coding style, but at least it runs.

FYI: std::map has two functions that might interest you: size() and empty(). No need to keep track of the size yourself.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement