Skip to content

Instantly share code, notes, and snippets.

@guanqun
Created August 7, 2013 15:40
Show Gist options
  • Save guanqun/6175227 to your computer and use it in GitHub Desktop.
Save guanqun/6175227 to your computer and use it in GitHub Desktop.
string split
/* 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