c++ primer中关于单词转换的程序
来源:互联网 发布:图像压缩算法代码 编辑:程序博客网 时间:2024/05/15 13:26
这是在第四版c++ primer书中第10章10.3.9的程序。觉得很好,记下来。
#include <iostream>#include <map>#include <fstream>#include <sstream>#include <string>#include <cstdlib>using namespace std;/*in1.txt中的内容'emthemcuzbecausegratzgratefuliInahnopossupposedsezsaidtanxthankswuzwasin2.txt中的内容nah i sez tanx cuz i wuz pos tonot cuz i wuz gratz*/ifstream& open_file(ifstream &in, const string &file) { in.close();//关闭以防之前已经打开 in.clear();//清除任何存在的错误 in.open(file.c_str()); return in;//此时in要么已经与文件绑定在了一起,要么处于错误的状态}int main(int argc, char **argv){ ifstream infile; ofstream outfile; map<string, string> wordChange; string input1 = "/home/xxx/Desktop/program/c++/单词转换/in1.txt"; string input2 = "/home/xxx/Desktop/program/c++/单词转换/in2.txt"; //infile.open(input1.c_str()); if(!open_file(infile, input1)) cout << "no file" << endl; string key, value; while(infile >> key >> value) wordChange[key] = value; string line, word; //ifstream input(input2.c_str()); ifstream input; if(!open_file(input, input2)) cout << "no file" << endl; map<string, string>::iterator iter; while(getline(input, line)) { istringstream stream(line); bool flag = true; while(stream >> word) { iter = wordChange.find(word);//此处不能用下标操作,因为下标操作会把原先没有的键值对加进去 if(flag) { flag = false; } else { cout << " "; } if(iter != wordChange.end()) cout << iter->second; else cout << word; } cout << endl; } infile.close(); return 0;}
总结:文件操作用的还不是很好。linux下文件路径的问题,要注意用各种命令检查路径和权限。如ls, pwd, stat, cat 等。
- c++ primer中关于单词转换的程序
- C++primer 第十章 单词转换程序 运行不了解决办法
- c++primer关联容器中的“单词转换map程序”分析
- C++Primer第11章 一个单词转换的map【程序】
- c++ primer 习题之 单词转换程序
- C++ Primer 关联容器 单词转换程序
- C++Primer第五版 练习11.38-2单词转换程序(解答)
- C++中map容器实现单词转换的程序
- C++Primer课后题11.33_单词本程序
- C++ primer中,"单词转换"map对象源码问题
- 《C++ Primer》P314中使用insert重写单词统计程序的扩展
- 计算字符串中有多少单词,并输出最长最短的单词。(c++primer 9.39)
- 一段解析字符串中单词的C语言小程序
- 对C++ Primer的10.3.9单词转换的思考
- 关于C++primer的疑问
- 使用map实现单词转换的程序
- 实现单词转换的小程序
- C++ primer 单词转换 map对象
- design mode is forbidden in FB4.6 with ApacheSDK
- autotools系列工具—自动生成Makefile
- org.apache.coyote.http11.Http11BaseProtocol 详解
- java web程序启动服务器时报的错误
- 在阿里云的云服务器CentOS系统安装Drupal内容管理框架
- c++ primer中关于单词转换的程序
- HDU 4655 Cut Pieces
- rabbitmq 管理
- 技术网站
- 循环队列
- 题目1
- Hive bucket的使用调研
- c++中::的用法
- [Tomcat] Coyote连接器框架源码分析