Help !高手过来看看 STL map 插入出错
#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;
void InitData()
{
for (int i = 0; i < 5; 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;
}
以上代码有个问题我在InitData()里面初始化一个全局map变量atrributeMap_g, 但是在PrintData()里面却得不到我刚才插入的元素,有时候是乱码,有时候只有一个值而且都是最后一个插入的值,前面的值都被冲调了。我查了些资料说是map的kay一定要不一样,代码里面是const char*, 每次比较的是字符串地址,而且每次该地址都是一样的,所以插入只会更新value值。
但是我想在不改变struct 定义的情况下,试图把const char* 的字符串的值作为Key插入,即:std::string strName(atrr.name), 然后以该字符串作为Key 插入到map里仍然和前面的情况一样。请问在不改变struct 和 map 定义的情况下,怎样正确循环插入元素。
#include<map>
#include<iostream>
using namespace std;
struct arrtibute
{
const char* name;
int id;
};
typedef std::map<std::string, arrtibute*> AtrributMap;
AtrributMap atrributeMap_g;
void InitData()
{
for (int i = 0; i < 5; 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;
}
以上代码有个问题我在InitData()里面初始化一个全局map变量atrributeMap_g, 但是在PrintData()里面却得不到我刚才插入的元素,有时候是乱码,有时候只有一个值而且都是最后一个插入的值,前面的值都被冲调了。我查了些资料说是map的kay一定要不一样,代码里面是const char*, 每次比较的是字符串地址,而且每次该地址都是一样的,所以插入只会更新value值。
但是我想在不改变struct 定义的情况下,试图把const char* 的字符串的值作为Key插入,即:std::string strName(atrr.name), 然后以该字符串作为Key 插入到map里仍然和前面的情况一样。请问在不改变struct 和 map 定义的情况下,怎样正确循环插入元素。
...全文
相关推荐
SGI STL Help Document.rar,SGL STL参考
SGI STL Help Document.rar,SGL STL参考资料。
vc_help.rar_Help!_VC-he_VC_HELP_帮助文档
vc++帮助文档,内包含5个chm文档。~~~~~~~~~~~~~~~~~~~~
C++STL库开发文档
查询一些熟悉又陌生的方法的参数与返回值、
C++函数STL速查手册.zip
C++ STL参考相当全面, 而且例子也很好. 为chm格式, 彩色, 目录和索引链接找东西很方便,还包含大量常用函数,且按函数名首字母排序,方便查找 也有 C 的函数速查手册哦!!
ja.rar_Help!
help you study c++,and it is very useful and helpful,wish you like it ,and it will help you



