C++ string和map容器实现简单的英文翻译
2013-11-29 21:45
573 查看
int main(int argc, const char *argv[]) { ifstream ifl; ifl.close(); ifl.clear(); string mapfilename("map.txt"); string transformname("transform.txt"); ifl.open(mapfilename.c_str()); string key,value; map<string,string> maptransform; while(ifl>>key>>value) { maptransform.insert(make_pair(key,value)); } ifl.close(); ifl.clear(); ostream::fmtflags oldflags=cout.flags(); map<string,string>::iterator iter=maptransform.begin(); cout<<"---------------------------------------"<<endl; cout<<"the map.txt is: "<<endl; while(iter!=maptransform.end()) { string tmp; cout<<setw(16)<<left; (tmp="key: ")+=iter->first; cout<<tmp; cout<<setw(16)<<left; (tmp="value: ")+=iter->second; cout<<tmp<<endl; ++iter; } cout.flags(oldflags); cout<<"---------------------------------------"<<endl; ifl.open(transformname.c_str()); string tmp,text; cout<<"the transformname.txt is: "<<endl; while(getline(ifl,tmp)) { cout<<tmp<<endl; text+=tmp+='\n'; } { string subtext(text); cout<<"transform transform.txt`s text by string::find " <<"and string::replace : "<<endl; for(iter=maptransform.begin();iter!=maptransform.end();++iter) { string::size_type pos=subtext.find(iter->first); if(pos!=string::npos) subtext.replace(pos,iter->first.size(),iter->second); } cout<<subtext; } { cout<<"---------------------------------------"<<endl; cout<<"transform transform.txt`s text " <<"by instead ostream : "<<endl; istringstream istm(text); string word; bool firstword=true; while(istm>>word) { map<string,string>::const_iterator iter= maptransform.find(word); if(iter!=maptransform.end()) word=iter->second; cout<<(firstword?(firstword=false,""):" ")<<word; } cout<<endl; } ifl.close(); ifl.clear(); system("pause"); return 0; }
相关文章推荐
- map容器的初步学习 -实现简单映射
- java实现简单的英文文本单词翻译器功能示例
- c++中map容器简单功能实现测试附带结构体指针测试
- [ios开发]利用有道翻译API实现简单的翻译功能
- Google Map开发(一) ASP.NET中调用Google Map API实现简单的地图显示
- C#调用微软在线翻译API实现简单的翻译功能
- 用java的spring实现一个简单的IOC容器示例代码
- 创建Windows Phone 7的试用版程序 简单实现方案(翻译)
- C#调用百度翻译API实现自己的简单翻译工具
- 一步步重构容器实现Spring框架——从一个简单的容器开始(八)
- 107-109_容器_自己实现HashMap_Map底层实现_哈希算法实现_使用数组和链表
- javascript实现简单的Map示例介绍
- 简单Spring容器实现
- C++中map容器实现单词转换的程序
- C++ string类简单实现
- 2. IOC 容器的简单实现(附源码)
- opencv对codebook的实现—自己加的简单英文解释
- Android文字图像识别并翻译的简单实现
- 重复造轮子--IOC容器的AOP简单实现
- 深入理解Spring--动手实现一个简单的SpringIOC容器-非注解版