Skip to content

Instantly share code, notes, and snippets.

@hitigon
Created March 15, 2014 03:38
Show Gist options
  • Save hitigon/9561517 to your computer and use it in GitHub Desktop.
Save hitigon/9561517 to your computer and use it in GitHub Desktop.
better string split for C++
// O(n)
int split(vector<string> &tokens, const string &str, const string &delim)
{
int count = 0;
auto begin = str.begin();
auto it = str.begin();
auto end = str.end();
while (begin != end && it != end) {
while (it != end && delim.find(*it) == string::npos) {
it++;
}
if (it - begin > 0) {
count++;
tokens.push_back(string(begin, it));
}
if (it != end) {
it++;
begin = it;
}
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment