利用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

你可能感兴趣的:(ios,String,iterator)