| | |
Help! STL map can not insert correctly
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
#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!
#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!
here atrr is declared as static as i result that the address that you are inserting into the vector is same everytime.
as a result when you do
you get the same values everytime.
can you print the address and confirm this?
•
•
•
•
insResult = atrributeMap_g.insert(std::make_pair(atrr.name, &atrr));
•
•
•
•
Itor->second->name;
Itor->second->id ;
can you print the address and confirm this?
Last edited by Agni; Apr 4th, 2008 at 2:33 am.
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
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.
The above code explains a lot.. try to comprehend what you are actually doing.
I don't know your level of C++, but assume you must be good enough, because you are using STL.
C++ Syntax (Toggle Plain Text)
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.
![]() |
Other Threads in the C++ Forum
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





