Created
February 25, 2017 21:11
-
-
Save donaldmunro/afa2f57bd4e52aa3cc0e3bbe422c889e to your computer and use it in GitHub Desktop.
Parse delimited string in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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