#include <string> 
#include <map> 
#include <iostream> 

using namespace std; 

struct arrtibute 
       const char* name; 
       int id; 

typedef std::map <std::string, arrtibute*> AtrributMap; 
AtrributMap atrributeMap_g; 
const int MaxCount = 3;

void InitData() 
       for (int i = 0; i  < MaxCount; i++) 

               static arrtibute atrr; 
               std::string str; 

               cout  < <"Input the name: "; 

               atrr.id = i; 
               atrr.name = str.c_str(); 

               std::pair <AtrributMap::iterator,bool> insResult; 
               insResult = atrributeMap_g.insert(std::make_pair(atrr.name, &atrr)); 

               // Make sure added successfully 
               if (!insResult.second) 
                      cout < <"insert to transfer map failed, id and name is: "  
                              < <atrr.id < <"  " < <atrr.name < <endl; 

void PrintData() 
       cout < <"**********print the map*********" < <endl; 
       AtrributMap::const_iterator Itor; 
       for( Itor = atrributeMap_g.begin(); 
       Itor != atrributeMap_g.end(); 
       ++Itor ) 
              cout < <"name is: " < <Itor->second->name < <" " < <"Id is : " < <
                    Itor->second->id < <endl; 
       cout < <"**********end of the map*********" < <endl; 

The above code have a problem: I insert 3 iterm into the global map atrributeMap_g, when I output the map there is an error:
all the 3 item have different value but the same id which is the last time insert id. I think this may caused by the key:atrr.name.
Since atrr.name is const char* which compare with the address in map key compare implementation, but I want to compare the string.
So I try to do std::string strName(atrr.name) to convert const char* to std::string then atrributeMap_g.insert(std::make_pair(strName, &atrr)),
but this take no effect.
How to loop to insert correctly under the conditon that do not change the definition of struct arrtibute and map AtrributMap.

Thanks a lot!

Edited by Dani: Formatting fixed

8 Years
Discussion Span
Last Post by Laiq Ahmed

it doesn't print any errors for me

one of the problems that you have of course is that you only have one "arrtibute" object in the entire program; so the values of the map all point to the same object


It can print but not the item I want.
Do you know how to insert the item correctly I input.
ps: Do not change the definition of struct arrtibute .


here atrr is declared as static as i result that the address that you are inserting into the vector is same everytime.

insResult = atrributeMap_g.insert(std::make_pair(atrr.name, &atrr));

as a result when you do

Itor->second->id ;

you get the same values everytime.
can you print the address and confirm this?


well I can see several errors.
I don't know your level of C++, but assume you must be good enough, because you are using STL.

struct attribute { 
	const char* name;
	int id;

int main () 
	static attribute attr;
	std::string str = "Laiq";
	attribute* attrPtr = 0;			//points to nothing...

	attr.name	 = str.c_str ();
	attr.id		 = 4;

	attrPtr = &attr;			// I point to attr here....

	cout<< attr.name<<endl;
	cout<< attr.id <<endl;
	cout<<"The address of Pointer 1 is :"<<attrPtr<<endl;

	str = "Ahmed";

	attribute* attrPtr2 = 0;
	attr.name = str.c_str ();
	attr.id = 18;

	attrPtr2 = &attr;

	cout<< attr.name<<endl;
	cout<< attr.id<<endl;
	cout<< "The address of Pointer 2 is : "<<attrPtr2<<endl;

	// Intersting.
	cout<<"The contents of both pointer are same....."<<endl;
	cout<<"The name of Ptr1 is \""<<attrPtr->name<< "\" and the name of Ptr2 is \""<<attrPtr2->name<<"\""<<endl;
	cout<<"The id of Ptr1 is \""<<attrPtr->id<< "\" and the id of Ptr2 is \""<<attrPtr2->id<<"\""<<endl;

	return 0;

The above code explains a lot.. try to comprehend what you are actually doing.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.