Last active
August 29, 2015 14:01
-
-
Save cporter/7fabb3f0afd714a224ec to your computer and use it in GitHub Desktop.
Splitting a string
This file contains hidden or 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
// -*- compile-command: "clang++ -std=c++1y split.cpp -o split" -*- | |
#include <iostream> | |
#include <string> | |
#include <sstream> | |
#include <algorithm> | |
#include <iterator> | |
#include <vector> | |
std::vector<std::string> split (const std::string& s) { | |
std::istringstream iss (s); | |
std::vector<std::string> out; | |
std::copy (std::istream_iterator<std::string> (iss), | |
std::istream_iterator<std::string> (), | |
std::back_inserter (out)); | |
return out; | |
} | |
int main (int, char **) { | |
std::string a { "It's the end of the world\tas we know it." }; | |
std::string b { " And I feel fine. " }; | |
auto va = split (a); | |
auto vb = split (b); | |
for (auto x : va) { | |
std::cout << x << "\n"; | |
} | |
for (auto x : vb) { | |
std::cout << x << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment