我有以下将对插入 STL 映射的函数。我需要在插入之前使用 new 分配内存吗?
char* foo(char* lnumber)
{
char* sData = “A,B,C”;
Char delim[] = “,”;
typedef std::map<std::string, std::string> TStrStrMap;
typedef std::pair<std::string, std::string> TStrStrPair;
TStrStrMap tMap;
if(strstr(sData,delim) != 0)
{
tok = strtok( sData, delim);
while( ( tok != NULL))
{
int bytes = strlen(tok)+1;
char* ll = new char[bytes];
memset(ll,0,bytes);
strcpy(ll,tok);
ll[bytes] = '\0';
int bytes1 = strlen("yes")+1;
char* ll1 = new char[bytes1];
memset(ll1,0,bytes1);
strcpy(ll1,”yes”);
ll1[bytes1] = '\0';
tMap.insert(TStrStrPair(ll,ll1));
tok = strtok( NULL, delim);
}
}
std::string strValue = tMap[lnumber];
return(strdup(strValue.c_str()));
}