forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.8.cpp
33 lines (30 loc) · 882 Bytes
/
11.8.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
// By using a `set`, we can avoid removing duplicates by ourself.
#include <vector>
#include <string>
#include <iterator>
#include <iostream>
#include <fstream>
#include <algorithm>
void elimDups(std::vector<std::string> &words) {
std::sort(words.begin(), words.end());
auto end_unique = std::unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
int main() {
std::string filename;
std::cin >> filename;
std::ifstream in(filename);
if (!in.is_open()) {
std::cerr << "Cannot open file: " << filename << std::endl;
return -1;
}
std::istream_iterator<std::string> i_iter(in), eof;
std::vector<std::string> words(i_iter, eof);
for (const auto &w : words)
std::cout << w << std::endl;
std::cout << "\nAfter remove duplicates:\n";
elimDups(words);
for (const auto &w : words)
std::cout << w << std::endl;
return 0;
}