#include <iostream>
#include <map>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#define FAILED 1
#define SUCCESS 0
using namespace std;
bool convert2Map(ifstream &inf, map<string, string> &ru);
bool emprassSecret(ifstream &inf, vector<vector<map<string, string>::mapped_type>> &svvec,
map<string, string> &ru);
int main()
{
ifstream inf1("rule.txt");
if (!inf1) {
cout << "Open the file is error" << endl;
return FAILED;
}
map<string, string> rules;
convert2Map(inf1, rules);
ifstream inf2("secret.txt");
if (!inf1) {
cout << "Open the file is error" << endl;
return FAILED;
}
vector<vector<map<string, string>::mapped_type>> svvec;
emprassSecret(inf2, svvec, rules);
for (auto svec : svvec) {
for (auto s : svec) {
cout << s << " ";
}
cout << endl;
}
return SUCCESS;
}
bool convert2Map(ifstream &inf, map<string, string> &ru) {
string line;
while (getline(inf, line)) {
auto pos = line.find(' ');
if (pos + 1 >= line.size()) {
return FAILED;
}
string s1(line, 0, pos);
string s2(line, pos + 1);
ru.insert(map<string, string>::value_type(s1, s2));
}
return SUCCESS;
}
bool emprassSecret(ifstream &inf, vector<vector<map<string, string>::mapped_type>> &svvec,
map<string, string> &ru) {
string line;
vector<vector<string>> temp;
while (getline(inf, line)) {
istringstream record(line);
string s;
vector<string> svec;
while (record >> s) {
svec.push_back(s);
}
temp.push_back(svec);
}
for (const auto &v: temp) {
vector<map<string, string>::mapped_type> svec;
for (const auto &s : v) {
if (ru.count(s)) {
svec.push_back(ru[s]);
}
else {
svec.push_back(s);
}
}
svvec.push_back(svec);
}
return SUCCESS;
}
P391--一个单词转换的map
最新推荐文章于 2022-12-20 02:21:54 发布