forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.11.cpp
34 lines (29 loc) · 989 Bytes
/
10.11.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
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
std::ostream &print(std::ostream &os, const std::vector<std::string> &vs) {
for (const auto &i : vs)
os << i << " ";
return os;
}
void elimDups(std::vector<std::string> &words) {
print(std::cout, words) << words.size() << std::endl;
std::sort(words.begin(), words.end());
print(std::cout, words) << words.size() << std::endl;
auto end_unique = std::unique(words.begin(), words.end());
print(std::cout, words) << words.size() << std::endl;
words.erase(end_unique, words.end());
print(std::cout, words) << words.size() << std::endl;
}
bool isShorter(const std::string &s1, const std::string &s2) {
return s1.size() < s2.size();
}
int main() {
std::vector<std::string> words;
for (std::string s; std::cin >> s; words.push_back(s)) {}
elimDups(words);
std::stable_sort(words.begin(), words.end(), isShorter);
print(std::cout, words) << words.size() << std::endl;
return 0;
}