您的位置:首页 > 编程语言 > C语言/C++

c++ primer 一个单词转换的map(自定义版)

2017-03-30 21:16 330 查看
#include<iostream>

#include<set>

#include<map>

#include<utility>

#include<string>

#include<fstream>

#include<sstream>

using namespace std;

map<string,string> tomap(const string &rule)

{
map<string, string> keys;
ifstream in(rule);
string line;
bool first = true;
string value, key;
while (getline(in, line))
{
istringstream is(line);
string word;
while (is >> word)
{
if (first)
{
key = word;
first = false;
}
else
{
value = word;
keys[key] = value;
first = true;
}
}
}
in.close();
return keys;

}

void translate(const string &rule, const string &trans,const string &tofile)

{
map<string,string> keys = tomap(rule);
ifstream in(trans);
string line;
ofstream os(tofile);
while (getline(in, line))
{
istringstream is(line);
string word;
while (is >> word)
{
auto every = keys.find(word);
if (every != keys.end())
os << every->second;
else
os << word;
os << " ";
}
}
os.close();
in.close();

}

int main()

{
translate("rule.txt", "translate.txt", "wh.txt");
system("pause");
return 0;

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