1 # include <iostream>
2 # include <string>
3 # include <iterator>
4 # include <vector>
5 # include <algorithm>
6 # include <sstream>
7
8 int main () {
9
10 std::string line1 = "We were her pride of 10 she named us --";
11 std::string line2 = "Benjamin, Pheonix, the Prodigal";
12 std::string line3 = "and perspicacious pacific Suzanne";
13
14 std::string sentence = line1 + ' ' + line2 + ' ' + line3;
15
16
17 std::string::size_type pos = 0;
18 std::string::size_type prepos = 0;
19 std::vector<std::string> words;
20
21 /** Method 1
22 * filter the space using find_first_of
23 **/
24 //while ((pos = sentence.find_first_of(" ",pos))
25 // != std::string::npos) {
26 // words.push_back(sentence.substr(prepos,(pos-prepos)));
27 // pos++;
28 // prepos = pos;
29 //}
30
31 /** Method 2
32 * filter the space using getline
33 **/
34 //std::stringstream ss(sentence);
35 //std::string temp;
36 //while(std::getline(ss,temp,' ')){
37 // words.push_back(temp);
38 //}
39
40
41 /** Method 3
42 * filter the space using <<
43 **/
44 std::stringstream ss(sentence);
45 std::string temp;
46 while(ss>>temp){
47 words.push_back(temp);
48 }
49
50
51 /////////// filter the punctuations /////////////////////////////////
52 std::string punctuations(",--10");
53 std::vector<std::string>::iterator iter;
54 // filter the punctuations
55 for (iter = words.begin(); iter != words.end(); ++iter) {
56 pos = 0;
57 while((pos = (*iter).find_first_of(punctuations,pos))
58 != std::string::npos) {
59 (*iter).erase(pos,1);
60 }
61 if((*iter).empty()) {
62 iter = words.erase(iter);
63 iter--;
64 }
65 }
66
67 // output the filtered vector
68 std::ostream_iterator<std::string> output(std::cout,"\n");
69 std::copy(words.begin(),words.end(),output);
70
71 return 0;
72 }