示例代码:
#include <iostream>
#include <map>
typedef struct TEST_STRUCT
{
int age;
int number;
}TEST, *PTEST;
std::map<int,PTEST> gMapTestPointer;
std::map<int,TEST> gMapTest;
void makeDataPointer()
{
//this is wrong
TEST t;
t.age = 20;
t.number = 10;
gMapTestPointer[1] = &t;
//this is ok
/*PTEST t = new TEST;
t->age = 20;
t->number = 10;
gMapTestPointer[1] = t;*/
}
PTEST getDataPointer(int id)
{
PTEST pt;
std::map<int,PTEST>::iterator iter = gMapTestPointer.find(id);
if(iter != gMapTestPointer.end())
{
pt = iter->second;
}
else
{
pt = NULL;
}
return pt;
}
void makeData()
{
TEST t;
t.age = 20;
t.number = 10;
gMapTest[1] = t;
}
TEST getData(int id)
{
TEST pt;
std::map<int,TEST>::iterator iter = gMapTest.find(id);
if(iter != gMapTest.end())
{
pt = iter->seco