代码来源于c++ primer 10.3

功能:已知一个一一对应的词典,求一小段文档对应的“翻译”

词典如下:

A a B b C c D d E e

输入:

D D E

代码:

//需要两个文件,一个是字典文件,一个是输入文件
#include <iostream>
#include <fstream>
#include <sstream>
#include <utility>
#include <map>
#include <string> using namespace std;
ifstream& open_file(ifstream &in, const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
int main(int argc,char ** argv)
{
map<string, string> trans_map;
string key, value;
if (argc != 3)
{
throw runtime_error("wrong number of arguments ,we need an dictionary.txt and an input.txt");
}
ifstream map_file;
if (!open_file(map_file,argv[1]))
{
throw runtime_error("no dictionary file");
}
while (map_file >> key >> value)
{
trans_map.insert(make_pair(key, value));
}
ifstream input;
if (!open_file(input, argv[2]))
{
throw runtime_error("no input file");
}
string line;
while (getline(input, line))
{
istringstream stream(line);
string word;
bool firstword = true;
while (stream >> word)
{
map<string, string>::const_iterator map_it = trans_map.find(word);
if (map_it != trans_map.end())
{
word = map_it->second;
}
if (firstword)
{
firstword = false;
}
else
{
cout << " ";
}
cout << word;
}
cout << endl;
}
return 0;
}

操作,makefile:

edit:trans_words.o
g++ -o edit trans_words.o
trans_words.o:trans_words.cpp
g++ -c trans_words.cpp clean:
rm trans_words.o

run.sh

#!/bin/sh
make
./edit dictionary.txt input.txt

结果:

d d e

最新文章

  1. Java 内存区域与内存溢出
  2. 【Android学习】android布局中几个距离单位的区别:px、dp、sp
  3. strace 监控所有php-fpm worker
  4. php--如何解决网站分页导致的SEO问题
  5. [CentOS 6.5 X64]讓firefox java plugin 啟動
  6. Mysql INNER,LEFT ,RIGHT join的使用
  7. [VC6 console]调用API获取手机归属地
  8. Hadoop学习笔记(两)设置单节点集群
  9. 热门IOS 第三方库
  10. 使用GitHub Pages+Jekyll搭建个人博客
  11. 【转】shell脚本实现多台服务器自动巡检--可参考学习
  12. Sybase数据库实现等效的mysql中group_concat功能
  13. year 和 weak year 的区别
  14. goroutine 知识点
  15. [ASP.NET MVC]笔记(四) UnobtruSive AJAX和客户端验证
  16. ZOJ 4062 Plants vs. Zombies(二分答案)
  17. [转]android sqlite db-journal文件产生原因及说明 .
  18. Android:进程优先级
  19. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
  20. java基础21 System类和Runtime类

热门文章

  1. 基于Redis的分布式锁和Redlock算法
  2. MacBook Pro 入手一年了,到底香不香?
  3. Pandas 数据分析,高中体测练习
  4. WIN10高清壁纸
  5. 20.用PyInstaller打包py程序的步骤及问题解决
  6. Linux之nohup命令
  7. 解决apt-get命令出现的安装源错误
  8. Java项目之客户信息管理软件
  9. 【 Tomcat 】tomcat8.0 基本参数调优配置-----(1)
  10. struts2 convention插件