forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex17_33.cpp
71 lines (60 loc) · 1.68 KB
/
ex17_33.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/***************************************************************************
* @file main.cpp
* @author qwert2603
* @date 13 Apr 2015
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <random>
using std::default_random_engine;
using std::uniform_int_distribution;
#include <ctime>
using std::time;
#include <algorithm>
using std::sort;
using std::find_if;
#include <utility>
using std::pair;
int main() {
typedef pair<string, string> ps;
ifstream i("d.txt");
vector<ps> dict;
string str1, str2;
// read wirds from dictionary
while (i >> str1 >> str2) {
dict.emplace_back(str1, str2);
}
i.close();
// sort words in vector
sort(dict.begin(), dict.end(), [](const ps &_ps1, const ps &_ps2){ return _ps1.first < _ps2.first; });
i.open("i.txt");
default_random_engine e(unsigned int(time(0)));
// read words from text
while (i >> str1) {
// find word in dictionary
vector<ps>::const_iterator it = find_if(dict.cbegin(), dict.cend(),
[&str1](const ps &_ps){ return _ps.first == str1; });
// if word doesn't exist in dictionary
if (it == dict.cend()) {
// write it itself
cout << str1 << ' ';
}
else {
// get random meaning of word
uniform_int_distribution<unsigned> u (0, find_if(dict.cbegin(), dict.cend(),
[&str1](const ps &_ps){ return _ps.first > str1; }) - it - 1);
// write random meaning
cout << (it + u(e))->second << ' ';
}
}
return 0;
}