将用逗号隔开的字符串存入MAP里

来源:互联网 发布:深圳市灵沃软件 编辑:程序博客网 时间:2024/06/06 08:26
map<int,int>CstringToIntMap(CString strSource)
{
map<int,int>serverIDMap;
if(strSource.IsEmpty())
return serverIDMap;

int nFind = -1,nIndex = 0;
nFind = strSource.Find(',');
if(nFind<0)
serverIDMap[atoi(strSource)] = atoi(strSource);
else
{
while(1)
{
serverIDMap[atoi(strSource.Left(nFind))] = atoi(strSource.Left(nFind));
strSource = strSource.Mid(nFind+1,strSource.GetLength());
nFind = strSource.Find(',');
if(nFind < 0)
{
serverIDMap[atoi(strSource)] = atoi(strSource);
break;
}
}
}
return serverIDMap;
}




用法:
map<int,int>serverIDListMap;
map<int,int>::iterator it;
serverIDListMap = CstringToIntMap(strServerIDList);


it = serverIDListMap.find(nServerID);
if(it != serverIDListMap.end())                                                      //如果找到
                     ......
0 0