C++ primer ————————————“单词转换" map 对象
2013-06-05 23:55
483 查看
#include<iostream> #include<map> #include<string> #include<utility> #include<fstream> #include<sstream> /* 单词转换 :给出一个string 对象 转换成另一个string对象 :输入是两个文件 第一个 含有若干单词对 做词典功能 : 第二个文件 提供了需要转换的文件。 */ using namespace std; typedef pair<string,string> str_str; int main() { map<string,string> trans_map; // 存放转换文件的内容 string key,value,word; string filename; char str[256]; // 存放从行中得到的数据 ifstream map_file; // 第一个文件 cout <<"Input the map file..."<< endl; cin >> filename; map_file.open(filename.c_str(),ios:: in); if(!map_file.is_open()) //鲁棒性 { cout <<"file cannot be open"<<endl; exit(1); } while(!map_file.eof()) { map_file.getline(str,256,'\n'); istringstream str_stream(str); str_stream >> key; str_stream >> value; trans_map.insert(str_str(key,value)); } ifstream file; cout <<"Input the file:"<<endl; cin >> filename; file.open(filename.c_str(),ios::in); if(!file.is_open()) //鲁棒性 { cout <<"file cannot be open"<<endl; exit(1); } while(!file.eof()) { file.getline(str,256,'\n'); string word; istringstream str_stream(str); bool firstword = true; while(str_stream >> word) { map<string,string>::iterator it = trans_map.find(word); if(it != trans_map.end()) word = it->second; if(firstword) firstword = false; else cout << " "; cout << word; } cout << endl; } system("pause"); return 0; }
相关文章推荐
- C++ primer中,"单词转换"map对象源码问题
- C++ 10.3.9 "单词转换"map对象
- C++ primer 单词转换(使用map对象)
- C++ Primer 第十章 10.3.9 “单词转换”map对象
- C++ primer 单词转换 map对象
- 使用eval转换json对象时为什么外边还要加括号eval("("+data+")");
- $("#id")与document.getElementById("id") 即 jQuery对象和DOM对象相互转换
- ArcEngine 无法将类型为"Systerm._ComObject"的对象强制转换为类型******
- 10.3.9例子“单词转换”map对象
- C++ Primer : 第十一章 : 关联容器示例: 一个单词转换的map
- 【C++ Primer】【练习题】【3.14】将vector<string>对象各元素中的小写字母转换成大写字母
- 单词转换(map对象)
- js中json字符串转换为对象以及转换是报 缺少";"错误的解决方法
- c++ primer 一个单词转换的map(自定义版)
- 单词转换程序——使用map&文件读写操作
- 个推推送时,Log4net 无法将类型"System.RuntimeType"的对象强制转换为类型"log4net
- 单词转换(map对象)
- JS:parseInt("08")或parseInt("09")转换返回0的原因
- c语言 宏操作,宏中"#"和"##"的用法,中间转换宏
- java操作<Map转换List——>list集合重新排序—>List集合转换成JSON>