Skip to content

Instantly share code, notes, and snippets.

@mattearly
Created April 16, 2017 00:36
Show Gist options
  • Save mattearly/d8afe122912eb8872bc0fddb62a32376 to your computer and use it in GitHub Desktop.
Save mattearly/d8afe122912eb8872bc0fddb62a32376 to your computer and use it in GitHub Desktop.
python-like split for c++
#pragma once
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
// split function for c++ -- from http://stackoverflow.com/questions/236129/split-a-string-in-c
template<typename Out>
void split(const std::string &s, char delim, Out result) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
*(result++) = item;
}
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment