bool cmp(const char& a)
{
static std::string space("\":,{}");
if(std::find(space.begin(), space.end(), a) != space.end())
{
return true;
}
return false;
}
map<string, float> cvt_str2map(string& str)
{
replace_if(str.begin(), str.end(), cmp, ' ');
stringstream ss(str);
map<string, float> result;
while(!ss.eof())
{
pair<string, float>p;
ss >> p.first;
ss >> p.second;
result.insert(p);
}
return result;
}

本文介绍了一种使用C++将字符串转换为键值对(map)的方法。通过定义一个辅助函数来过滤掉字符串中的特殊字符,然后利用istringstream进行解析,最终实现了从字符串到map的转换。
1247

被折叠的 条评论
为什么被折叠?



