Parse string to create a list of element
Posted
by
Nick
on Stack Overflow
See other posts from Stack Overflow
or by Nick
Published on 2012-07-02T19:55:46Z
Indexed on
2012/09/26
15:37 UTC
Read the original article
Hit count: 272
I have a string like this:
"\r color=\"red\" name=\"Jon\" \t\n depth=\"8.26\" "
And I want to parse this string and create a std::list
of this object:
class data
{
std::string name;
std::string value;
};
Where for example:
name = color
value = red
What is the fastest way? I can use boost.
EDIT:
This is what i've tried:
vector<string> tokens;
split(tokens, str, is_any_of(" \t\f\v\n\r"));
if(tokens.size() > 1)
{
list<data> attr;
for_each(tokens.begin(), tokens.end(), [&attr](const string& token)
{
if(token.empty() || !contains(token, "="))
return;
vector<string> tokens;
split(tokens, token, is_any_of("="));
erase_all(tokens[1], "\"");
attr.push_back(data(tokens[0], tokens[1]));
}
);
}
But it does not work if there are spaces inside " "
: like color="red 1"
.
© Stack Overflow or respective owner