Help! STL map can not insert correctly

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 2
Reputation: yonex is an unknown quantity at this point 
Solved Threads: 0
yonex yonex is offline Offline
Newbie Poster

Help! STL map can not insert correctly

 
0
  #1
Apr 3rd, 2008
#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: ";
cin>>str;

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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Help! STL map can not insert correctly

 
0
  #2
Apr 3rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 2
Reputation: yonex is an unknown quantity at this point 
Solved Threads: 0
yonex yonex is offline Offline
Newbie Poster

Re: Help! STL map can not insert correctly

 
0
  #3
Apr 4th, 2008
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 .
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 449
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Help! STL map can not insert correctly

 
0
  #4
Apr 4th, 2008
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->name;
Itor->second->id ;
you get the same values everytime.
can you print the address and confirm this?
Last edited by Agni; Apr 4th, 2008 at 2:33 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Help! STL map can not insert correctly

 
0
  #5
Apr 4th, 2008
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.

  1.  
  2.  
  3. struct attribute {
  4. const char* name;
  5. int id;
  6. };
  7.  
  8.  
  9. int main ()
  10. {
  11. static attribute attr;
  12. std::string str = "Laiq";
  13.  
  14. attribute* attrPtr = 0; //points to nothing...
  15.  
  16. attr.name = str.c_str ();
  17. attr.id = 4;
  18.  
  19. attrPtr = &attr; // I point to attr here....
  20.  
  21. cout<< attr.name<<endl;
  22. cout<< attr.id <<endl;
  23. cout<<"The address of Pointer 1 is :"<<attrPtr<<endl;
  24.  
  25. str = "Ahmed";
  26.  
  27. attribute* attrPtr2 = 0;
  28. attr.name = str.c_str ();
  29. attr.id = 18;
  30.  
  31.  
  32. attrPtr2 = &attr;
  33.  
  34. cout<< attr.name<<endl;
  35. cout<< attr.id<<endl;
  36. cout<< "The address of Pointer 2 is : "<<attrPtr2<<endl;
  37.  
  38. // Intersting.
  39. cout<<"The contents of both pointer are same....."<<endl;
  40. cout<<"The name of Ptr1 is \""<<attrPtr->name<< "\" and the name of Ptr2 is \""<<attrPtr2->name<<"\""<<endl;
  41. cout<<"The id of Ptr1 is \""<<attrPtr->id<< "\" and the id of Ptr2 is \""<<attrPtr2->id<<"\""<<endl;
  42.  
  43. return 0;
  44. }

The above code explains a lot.. try to comprehend what you are actually doing.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC