64,642
社区成员
发帖
与我相关
我的任务
分享
char * cc = "H=153004;P=0;JOB=180573866;ID=2;DIFF=0.00327355;N=0x1b27027;FROM=0xbef7d7;SCANTO=0x8806087;COUNT=0x7c168b0;"
#include <vector>
#include <string>
#include <map>
template <typename Container>
Container& split(Container& result,
const typename Container::value_type& s,
const typename Container::value_type& delimiters)
{
result.clear();
size_t current;
size_t next = -1;
do
{
current = next + 1;
next = s.find_first_of(delimiters, current);
result.push_back(s.substr(current, next - current));
} while (next != Container::value_type::npos);
return result;
}
void StringToMap(std::map<std::string, std::string>& result, const std::string& s)
{
result.clear();
std::vector<std::string> fields;
std::vector<std::string> keyvals;
split(fields, s, ";");
for (auto f : fields)
{
if (!f.empty())
{
split(keyvals, f, "=");
if (!keyvals.empty() && keyvals.size() == 2)
{
result[keyvals[0]] = keyvals[1];
}
}
}
}
int main()
{
char * cc = "H=153004;P=0;JOB=180573866;ID=2;DIFF=0.00327355;N=0x1b27027;FROM=0xbef7d7;SCANTO=0x8806087;COUNT=0x7c168b0;";
std::map<std::string, std::string> m;
StringToMap(m, cc);
return 0;
}
这个是把你所列字符串转到map里面的函数,键和值都是字符串类型,不知道是不是你需要的。