Created
August 7, 2013 15:40
-
-
Save guanqun/6175227 to your computer and use it in GitHub Desktop.
string split
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
/* extracted from http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html | |
* use this hand-crafted function if boost can't be used in the project. | |
*/ | |
void tokenize(const string& str, | |
vector<string>& tokens, | |
const string& delimiters = " ") | |
{ | |
// Skip delimiters at beginning. | |
string::size_type lastPos = str.find_first_not_of(delimiters, 0); | |
// Find first "non-delimiter". | |
string::size_type pos = str.find_first_of(delimiters, lastPos); | |
while (string::npos != pos || string::npos != lastPos) | |
{ | |
// Found a token, add it to the vector. | |
tokens.push_back(str.substr(lastPos, pos - lastPos)); | |
// Skip delimiters. Note the "not_of" | |
lastPos = str.find_first_not_of(delimiters, pos); | |
// Find next "non-delimiter" | |
pos = str.find_first_of(delimiters, lastPos); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment