C++Primer CH11关联容器

在这里插入图片描述

#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<map>
#include<set>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::set;
using std::ifstream;
using std::ofstream;
using std::istringstream;
using std::ostringstream;

map<string, string> build_rule_map(ifstream& infile) {
	string line;
	map <string, string> rule_map;
	while ( getline(infile, line)) {
		istringstream Inline(line);
		string map_key;
		Inline >> map_key; //把第一个单词作为Key输入给 map_key;

		//把后面的字符串存为map_value,注意保留字符串
		string map_value;
		string single_value;
		bool first_string = true;
		while (Inline >> single_value) {
			if (first_string)
			{
				map_value = single_value;
				first_string = false;
			}
			else
				map_value = map_value + " " + single_value;
		}
		//存入map中
		rule_map[map_key] = map_value;
	}
	return rule_map;
}

//单个单词查找查找替换函数
string transform(string& s, map<string, string> rule_map) {
	for (auto beg = rule_map.lower_bound(s), end = rule_map.upper_bound(s); beg != end; ++beg) {
		if (beg->first == s)
		{
			s = beg->second;
			return s;
		}
	}
	return s;
}
void transform_word (const string rule_txt,const string test_txt,const string out_txt) {
	//创建一个输出文件
	ofstream out(out_txt, ofstream::app);
	//从规则文件中生成,规则map
	ifstream in(rule_txt);
	if (!in.good())
	{
		try { throw std::runtime_error("rule txt  file is not exist"); }
		catch (std::runtime_error err) { cout << err.what() << endl; return; }
	}

	map<string, string> rule_map = build_rule_map(in);

	//讲测试文件的词句更改调整
	ifstream test(test_txt);
	if (!test.good())
	{
		try { throw std::runtime_error("test txt  file is not exist"); }
		catch (std::runtime_error err) { cout << err.what() << endl; return; }
	}
	//逐行读取文本
	string line;
	while (getline(test, line)) {
		istringstream lineStr(line);
		//一个单词一个单词检测
		string word;
		string lineword;//存储修改后的一行
		bool first_world = true;
		while (lineStr >> word)
		{
			word = transform(word, rule_map);
			//将修改后的单词重新组合成一行
			if (first_world) {
				lineword = word;
				first_world = false;
			}
			else
				lineword = lineword + " " + word;
		}
		//讲一行新的句子写入到文本中
		out << lineword << endl;
	}
	out.close();
}




int main() {
	//***************************************test Program11.1
	//map<string, size_t> word_count;
	//set<string> exclude = { ",","." };
	//string s;
	//cout << "enter some string: " << endl;
	//while (cin>>s)
	//{
	//	if (exclude.find(s) == exclude.end())
	//		++word_count[s];
	//}

	//cout << "the result of statistic: " << endl;
	//for (const auto& word : word_count) {
	//	cout << word.first << " occurs:  " << word.second << ((word.second>1)?" time.":" times.") << endl;
	//}

	//*******************************test program 11.33


	string rule_txt = "rule_map.txt";
	string test_txt = "test.txt";
	string out_txt = "out.txt";

	transform_word(rule_txt, test_txt, out_txt);

	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值