-
Notifications
You must be signed in to change notification settings - Fork 1
/
fscr.cpp
79 lines (70 loc) · 2.01 KB
/
fscr.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
72
73
74
75
76
77
78
79
#include <iostream>
#include <fstream>
#include <istream>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
//fast_scrable
// g++ -Ofast -Wall -Werror -std=c++11 -o ./fscr *.cpp
// ./fscr -w ficher.txt
std::istream& safeGetline(std::istream& is, std::string& t)
{
t.clear();
std::istream::sentry se(is, true);
std::streambuf* sb = is.rdbuf();
for(;;) {
int c = sb->sbumpc();
switch (c) {
case '\n':
return is;
case '\r':
if(sb->sgetc() == '\n')
sb->sbumpc();
return is;
case EOF:
if(t.empty())
is.setstate(std::ios::eofbit);
return is;
default:
t += (char)c;
}
}
}
multimap<string,string> load_file_in_set(string path_file)
{
multimap<string,string> multimap_dico;
ifstream dict_file(path_file.c_str());
string line;
while (!safeGetline(dict_file, line).eof()) {
string new_line = line;
transform(line.begin(), line.end(), line.begin(), ::tolower);
string tmp_newline = line;
sort(tmp_newline.begin(), tmp_newline.end());
multimap_dico.insert(make_pair(tmp_newline, new_line));
}
return multimap_dico;
}
int main(int argc, char **argv)
{
/* Load in Set */
multimap<string,string> mymultimap;
multimap<string,string>::iterator it;
mymultimap = load_file_in_set(argv[2]);
string return_user_input;
while(true)
{
/* Read Input User */
cin >> return_user_input;
string tmp = return_user_input;
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
sort(tmp.begin(), tmp.end());
pair<multimap<string,string>::iterator, multimap<string,string>::iterator> ii;
ii = mymultimap.equal_range(tmp);
for(it = ii.first; it != ii.second; ++it)
{
cout<<it->second<<endl;
}
}
return 0;
}