Skip to content

Instantly share code, notes, and snippets.

@wusuopubupt
Created February 3, 2017 03:47
Show Gist options
  • Save wusuopubupt/febf6379c68ca5368bc17f4e3b0eccd2 to your computer and use it in GitHub Desktop.
Save wusuopubupt/febf6379c68ca5368bc17f4e3b0eccd2 to your computer and use it in GitHub Desktop.
#include<string>
#include<vector>
#include<sstream>
#include<iostream>
std::vector<std::string> split(const std::string& s, char delimiter) {
std::string item;
std::istringstream is(s);
std::vector<std::string> ret;
while(std::getline(is, item, delimiter)) {
ret.push_back(item);
}
return ret;
}
int main(int argv, char **argc) {
std::string s = "hello,world,123";
char delimiter = ',';
std::vector<std::string> tokens;
tokens = split(s, delimiter);
int i = 0;
while( i < tokens.size() ) {
std::cout << tokens[i] << std::endl;
i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment