Created
March 15, 2014 03:38
-
-
Save hitigon/9561517 to your computer and use it in GitHub Desktop.
better string split for 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
// 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