-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-4.cpp
More file actions
39 lines (33 loc) · 824 Bytes
/
example-4.cpp
File metadata and controls
39 lines (33 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>
// https://stackoverflow.com/questions/236129/the-most-elegant-way-to-iterate-the-words-of-a-string
template <typename Out>
void split(const std::string &s, char delim, Out result)
{
std::stringstream ss(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;
}
int main()
{
std::vector<std::string> x = split("who is on duty today?", ' ');
std::reverse(x.begin(), x.end());
for (auto &word : x)
{
std::cout << word << std::endl;
}
return 0;
}