Skip to content

Instantly share code, notes, and snippets.

@octavifs
Created June 7, 2013 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save octavifs/5728604 to your computer and use it in GitHub Desktop.
Save octavifs/5728604 to your computer and use it in GitHub Desktop.
Split c++ string by char delimiter. Return vector with tokens. This solution does not skip empty tokens. Based on http://stackoverflow.com/a/236803
#include <string>
#include <sstream>
#include <vector>
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim=' ') {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment