Skip to content

Instantly share code, notes, and snippets.

@donaldmunro
Created February 25, 2017 21:11
Show Gist options
  • Save donaldmunro/afa2f57bd4e52aa3cc0e3bbe422c889e to your computer and use it in GitHub Desktop.
Save donaldmunro/afa2f57bd4e52aa3cc0e3bbe422c889e to your computer and use it in GitHub Desktop.
Parse delimited string in C++
size_t split(std::string s, std::vector<std::string>& tokens,
std::string delim ="\t\n ")
{
tokens.clear();
size_t pos = s.find_first_not_of(delim);
while (pos != std::string::npos)
{
size_t next = s.find_first_of(delim, pos);
if (pos == std::string::npos)
tokens.emplace_back(s.substr(pos));
else
{
tokens.emplace_back(s.substr(pos, next-pos));
pos = s.find_first_not_of(delim, next);
}
}
return tokens.size();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment