Skip to content

Instantly share code, notes, and snippets.

@luqiang21
Last active April 13, 2020 00:07
Show Gist options
  • Save luqiang21/d9c5aef9c951355c2503de682b75190f to your computer and use it in GitHub Desktop.
Save luqiang21/d9c5aef9c951355c2503de682b75190f to your computer and use it in GitHub Desktop.
c++ string split
#include <string>
#include <vector>
void split(vector<string>& splitted, string& pattern, char delimiter) {
if (pattern.size() == 0) {
splitted.push_back(pattern);
return;
}
int start = 0, i;
for (i = 0; i < pattern.size(); ++i) {
char ch = pattern[i];
if (ch == delimiter) {
splitted.push_back(pattern.substr(start, i - start));
start = i + 1;
}
}
if (start == 0) {
splitted.push_back(pattern);
return;
}
splitted.push_back(pattern.substr(start, i + 1 - start));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment