您的位置:首页 > 其它

P391--一个单词转换的map

2014-11-16 22:13 357 查看
#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  map