-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathExer10_11.cpp
More file actions
60 lines (60 loc) · 1.6 KB
/
Exer10_11.cpp
File metadata and controls
60 lines (60 loc) · 1.6 KB
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
60
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
using std::string;
using std::vector;
inline bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
for(const auto &s : words)
cout << s << " ";
cout << endl;
words.erase(end_unique, words.end());
}
vector<string>::iterator elimDups(vector<string>::iterator beg, vector<string>::iterator end)
{
sort(beg, end);
auto end_unique = unique(beg, end);
return end_unique;
}
int main(int argc, char* argv[])
{
if(argc != 2)
{
cerr << "Wrong input argument." << endl;
return -1;
}
ifstream is(argv[1]);
string word;
vector<string> text;
while(is >> word)
{
text.push_back(word);
}
vector<string>::size_type part= text.size() > 50 ? 50 : text.size();
for(auto iter = text.begin(); iter != text.begin() + part; ++iter)
cout << *iter << " ";
cout << "\n" << endl;
auto end_unique = elimDups(text.begin(), text.begin() + part);
text.erase(end_unique, text.end());
for(auto iter = text.begin(); iter != end_unique; ++iter)
cout << *iter << " ";
cout << "\n" << endl;
stable_sort(text.begin(), text.end(), isShorter);
for(auto iter = text.begin(); iter != end_unique; ++iter)
cout << *iter << " ";
cout << "\n" << endl;
return 0;
}