Skip to content

Instantly share code, notes, and snippets.

@abc126
Created October 31, 2019 03:24
Show Gist options
  • Select an option

  • Save abc126/d4856fa6ea2a0b142a9d2053dfb66486 to your computer and use it in GitHub Desktop.

Select an option

Save abc126/d4856fa6ea2a0b142a9d2053dfb66486 to your computer and use it in GitHub Desktop.
split string c++
void split(const string& s, vector<string>& tokens, const string& delimiters = " ")
{
string::size_type lastPos = s.find_first_not_of(delimiters, 0);
string::size_type pos = s.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos) {
tokens.push_back(s.substr(lastPos, pos - lastPos));//use emplace_back after C++11
lastPos = s.find_first_not_of(delimiters, pos);
pos = s.find_first_of(delimiters, lastPos);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment