Skip to content

Instantly share code, notes, and snippets.

@cporter
Last active August 29, 2015 14:01
Show Gist options
  • Save cporter/7fabb3f0afd714a224ec to your computer and use it in GitHub Desktop.
Save cporter/7fabb3f0afd714a224ec to your computer and use it in GitHub Desktop.
Splitting a string
// -*- 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