利用istringstream分割字符串

Description of the problem:

      Katz is a little boy who is very funny and very interesting in reading fairy tale. When he finished reading a book, he always want to find out the different words in the book, and then he counts the words whose length is no less than a specific number.  It's a time-consuming work for he to do, so he want you to write out a program then it can automatically accomplish the task. 

        For simplicity, there is no punctuations in the text of the book and these words are case sensitive. Note that there may be multiple whitespace between two words and the beginning and ending of the text may contain whitespace. Remember your task is to write a class which can count different words whose length is not less than a specific number(It will be initialized in constructor and can be changed). 

      Please read the following test framework and its output, implement the WordsCounter.

Sample Input//test framework
#include <iostream>
#include "source.cpp"
using namespace std;

int main()
{
	WordsCounter wordsCounter(5);
	cout << wordsCounter("It is so interesting that I like it greatly") << endl;
	
	string text = "it is so interesing that I like it greatly";
	wordsCounter.setBound(2);
	cout << wordsCounter(text);
	
	return 0;
}
在不用istringstream的时候,在分解字符串是会变的比较复杂,也容易出错,下面是不用istringstream时的代码:
 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <vector>
 5 
 6 using namespace std;
 7 
 8 class WordsCounter{
 9 public:
10     WordsCounter(int n = 0):count(n){count_ = 0;}
11     ~WordsCounter(){}
12     WordsCounter& operator()(string str){
13         sevc.clear();
14         count_ = 0;
15         int k = 0;
16         while(!str.empty()){
17             for(int i = 0;i <= str.size();i++){
18                 if(str[i] == ' '){}
19                 else{
20                     str.erase(0, i);
21                     break;
22                 }
23                     
24             }
25             string temp1(str);
26             for(int i = 0;i <= temp1.size();){
27                 if(i == temp1.size() || temp1[i] == ' '){
28                     string temp(str,0, i - k);
29                     sevc.push_back(temp);
30                     while(temp1[i] == ' '){
31                         i++;
32                     }
33                     str.erase(0, i - k);
34                     k = i;
35                     if(i == temp1.size())
36                         break;
37                 }
38                 else{
39                     i++;
40                 }
41             }
42         }
43         sort(sevc.begin(), sevc.end());
44         string temp = sevc[0];
45         if(sevc[0].size() >= count)
46             count_ = 1;
47         for(int i = 1;i != sevc.size();i++){
48             if(sevc[i] != temp){
49                 temp = sevc[i];
50                 if(sevc[i].size() >= count)
51                     count_++;
52             }
53         }
54         return *this;
55     }
56     friend ostream& operator <<(ostream& os, WordsCounter& temp){
57         os << temp.count_;
58         return os;
59     }
60     void setBound(int te){
61         count = te;
62     }
63 private:
64     int count;
65     int count_;
66     vector<string> sevc;
67 };                                 

但用了istringstreastream后,代码可以简化成这样了:

 1 #include <iostream>
 2 #include <string>
 3 #include <sstream>
 4 #include <set>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 class GT_N {
 9 public:
10     GT_N(int _count = 0): count(_count){}
11     bool operator()(const string& s) {
12         return s.length() >= count;
13     }
14     int count;
15 };
16 
17 class WordsCounter{
18 public:
19     WordsCounter(int n = 0):gt_n(n){}
20     ~WordsCounter(){}
21 
22     int operator()(const string& str){
23         set<string> substrings;
24         istringstream iss(str);
25         string tmp;
26         while(iss >> tmp) {
27             substrings.insert(tmp);
28         }
29 
30         return count_if(substrings.begin(), substrings.end(), gt_n);
31     }
32 
33     void setBound(int te){
34         gt_n.count = te;
35     }
36 private:
37     GT_N gt_n;
38 };

istringstream对象可以绑定一行字符串,然后以空格为分隔符把该行分隔开来。这样的话在处理字符串的时候会变的非常方便的。。。

 
原文地址:https://www.cnblogs.com/xiezhw3/p/3096400.html