6.3w+
社区成员
#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 ReleaseData()
{
for (AtrributMap::iterator i = atrributeMap_g.begin();
i != atrributeMap_g.end(); ++i)
{
delete[] i->second.name;
}
}
void InitData()
{
for (int i = 0; i < MaxCount; i++)
{
arrtibute atrr;
memset(&atrr, 0, sizeof(arrtibute));
std::string str;
cout <<"Input the name: ";
cin>>str;
atrr.id = i;
char* temp = new char[128];
strcpy(temp, str.c_str());
atrr.name = temp;
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;
}
void main()
{
InitData();
PrintData();
ReleaseData();
}