利用map容器进行读入文件并对响应的字符进行转换并输出转换后的字符

 
#include<iostream>
#include<fstream>
#include<map>
#include<string>
using namespace std;
int main()
{
	map<string,string>transmap;
	ifstream is;
	is.open("E:\\coding\\String\\Debug\\2.txt");
	char st[100]={0};
	while(is>>st)
	{
		string key=st;
		is>>st;
		string value=st;
		transmap[key]=value;
	}
	map<string,string>::iterator it;
	//检测输入是否成功
	for(it=transmap.begin();it!=transmap.end();it++)
	{
		cout<<it->first<<"  "<<it->second<<endl;
	}
	is.close();
	ifstream ip;
	ofstream os;
	os.open("E:\\coding\\String\\Debug\\1.txt",ios::app);
	os<<endl;
	ip.open("E:\\coding\\String\\Debug\\1.txt");
	while(ip>>st)
	{
		it=transmap.find(st);
		if(it!=transmap.end())
		{
			os<<it->second<<" ";
		}
		else
			os<<st<<" ";
	}
	os.close();
	ip.close();
	return 0;
}


该代码是将2.txt中定义的需要转换的字符对读取到map容器中,读入1.txt中的文本,按照map中映射的字符对1.TXT中的响应字符修改掉

其中2.TXT中的内容为

'em  them
cuz  because
gratz  grateful
i  I
nah  no
pos  supposed
sez  said
tanx  thanks
wuz  was

1.txt中的文本为

nah i sez tanx cuz i wuz pos to not cuz i wuz gratz

进行转换后的文字为:no I said thanks because I was supposed to not because I was grateful

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值