-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
52 lines (47 loc) · 1.09 KB
/
Main.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
#include<iostream>
#include<fstream>
#include<string>
#include<map>
#include<sstream>
void word_transform(std::ifstream& map_file, std::ifstream& input)
{
auto trans_map = buildMap(map_file);
std::string text;
while (getline(input, text)) {
std::istringstream stream(text);
std::string word;
bool firstword = true;
while (stream >> word){
if (firstword)
firstword = false;
else
std::cout << " ";
std::cout << transform(word, trans_map);
}
std::cout << std::endl;
}
}
std::map<std::string, std::string> buildMap(std::ifstream& map_file)
{
std::map<std::string, std::string> trans_map;
std::string key;
std::string value;
while (map_file >> key && getline(map_file, value))
if (value.size() > 1)
trans_map[key] = value.substr(1);
else
throw std::runtime_error("no rule for " + key);
return trans_map;
}
const std::string &
transform(const std::string& s, const std::map<std::string,std::string> &m)
{
auto map_it = m.find(s);
if (map_it != m.cend())
return map_it->second;
else
return s;
}
int main()
{
}