-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExer14_39.cpp
37 lines (37 loc) · 945 Bytes
/
Exer14_39.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using std::cout;
using std::endl;
using std::ifstream;
using std::string;
using std::vector;
class InRange {
public:
InRange(string::size_type d, string::size_type u) : down(d), up(u) {}
bool operator()(const string& s) const
{ return s.size() >= down && s.size() <= up; }
private:
string::size_type down;
string::size_type up;
};
int main(int argc, char* argv[])
{
if(argc != 2)
return -1;
ifstream file(argv[1]);
string word;
vector<string> text;
while(file >> word)
text.push_back(word);
for(string::size_type upper = 2; upper != 20; ++upper)
{
auto cnt = count_if(text.cbegin(), text.cend(), InRange(1, upper));
cout << "There are " << cnt
<< " words whose length between 1 and " << upper
<<" inclusive" << endl;
}
return 0;
}