Created
April 16, 2017 00:36
-
-
Save mattearly/d8afe122912eb8872bc0fddb62a32376 to your computer and use it in GitHub Desktop.
python-like split for c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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