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;
}
#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++ Primer : 第十一章 : 关联容器示例: 一个单词转换的map
- C++ primer中,"单词转换"map对象源码问题
- C++ Primer 第十章 10.3.9 “单词转换”map对象
- 一个单词转换的map
- P391--一个单词转换的map
- C++Primer第11章 一个单词转换的map【程序】
- C++ primer 单词转换 map对象
- C++ primer 单词转换(使用map对象)
- C++ primer ————————————“单词转换" map 对象
- 单词转换(map对象)
- c++primer关联容器中的“单词转换map程序”分析
- 对C++ Primer的10.3.9单词转换的思考
- 给出一句英文句子: "let there be light" 得到一个新的字符串,每个单词的首字母都转换为大写
- 《C++ Primer》十章练习题单词转换word_transform.cpp
- java 将一个listBean转换为map
- 使用map关联容器实现单词转换的程序
- 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456
- Bean-Query 一个把对象转换为Map的Java工具库
- 如何将一个map 转换成json数据
- map——单词的转换