forked from orazaro/accelerated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-00-generic-split.cpp
59 lines (48 loc) · 1.1 KB
/
08-00-generic-split.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
using namespace std;
bool not_space(char c)
{
return !isspace(c);
}
bool space(char c)
{
return isspace(c);
}
template <class Out>
void split(const string& str, Out os)
{
typedef string::const_iterator iter;
iter i = str.begin();
while( i != str.end()) {
// ignore leading blanks
i = find_if(i, str.end(), not_space);
// find end of next word
iter j = find_if(i, str.end(), space);
// copy the characters in [i,j)
if( i != str.end())
*os++ = string(i,j);
i = j;
}
}
int main()
{
string s;
vector<string> v;
while(getline(cin, s)) {
split(s, back_inserter(v));
}
cout << "result: ";
copy(v.begin(),v.end(),ostream_iterator<string>(cout," "));
cout << endl;
stringstream ss;
copy(v.begin(),v.end(),ostream_iterator<string>(ss," "));
s = ss.str();
cout << "result2: " << s << endl;
cout << "result3: " << endl;
split(s, ostream_iterator<string>(cout,"\n"));
return 0;
}