代码来源于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

map实现单词转换程序的例子的更多相关文章

  1. 自定义实现InputFormat、OutputFormat、输出到多个文件目录中去、hadoop1.x api写单词计数的例子、运行时接收命令行参数,代码例子

    一:自定义实现InputFormat *数据源来自于内存 *1.InputFormat是用于处理各种数据源的,下面是实现InputFormat,数据源是来自于内存. *1.1 在程序的job.setI ...

  2. map集合修改其中元素 去除Map集合中所有具有相同值的元素 Properties长久保存的流操作 两种用map记录单词或字母个数的方法

    package com.swift.lianxi; import java.util.HashMap; import java.util.Iterator; import java.util.Map; ...

  3. 关于MapReduce单词统计的例子:

    要统计的文件的文件名为hello hello中的内容如下 hello you hello me 通过MapReduce程序统计出文件中的各个单词出现了几次.(两个单词之间通过tab键进行的分割) im ...

  4. C++primer 练习11.33:实现你自己版本的单词转换程序

    // 11_33.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  5. poj1002 字典树+map+查询单词出现次数

    487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 309235   Accepted: 55223 Descr ...

  6. 字符拆分存入Map计算单词的个数

    ///计算从命令行输入单词的种类与个数//Map<key,Value>Key-->单词:Value-->数量

  7. Android List<Map<String,String>转json(例子)

    package com.armslee.json.test.cases; import java.util.ArrayList; import java.util.HashMap; import ja ...

  8. Storm官方提供的trident单词计数的例子

    上代码: public class TridentWordCount { public static class Split extends BaseFunction { @Override publ ...

  9. PTA1071 - Speech Patterns - map计算不同单词个数

    题意 输出给定字符串出现最多的字符串(小写输出)和出现次数. 所求字符串要求:字符中可以含有A-Z.0-9. 比如说题目给出的Can1,我们可以转换成can1,can1就算一个字符串整体,而不是单独的 ...

随机推荐

  1. Spring in action读书笔记(一) 自动化装配bean

    需要引入的ja包 <dependency> <groupId>org.springframework</groupId> <artifactId>spr ...

  2. NET Core 3.1 PATCH HTTP 的使用注意事项

    使用Postman请求示例: 一.在Headers要声明请求类型Content-Type 二.body提交要使用raw,且声明为json格式传输 三.如果有authorization验证还需要带上(如 ...

  3. PrototypePattern(原型模式)-----Java/.Net

    原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式.

  4. 使用ASP.NET Core 3.x 构建 RESTful API - 4.3 HTTP 方法的安全性和幂等性

    什么样的HTTP方法是安全的? 如果一个方法不会该表资源的表述,那么这个方法就被认为是安全的. 例如 HTTP GET 和 HTTP HEAD 就被认为是安全的,但需要注意的是,这并不意味着执行GET ...

  5. 突破CRUD | 简单优雅的代码生成工具诞生记(万字长文慎入)

    0.学习本文你或许可以收获 1.一个需求从产生.分析到解决的全过程思考2.简单的面向对象分析实践3.UML类图实践4.设计模式的实践应用5.最后收获一款还算不错的代码生成工具实现思路和源代码 本文将从 ...

  6. Java字符串(String类)

    定义方法: 1.String demo = "test"; 2.String demo = new String(); 3.String demo = new String(&qu ...

  7. vue实现网络图片瀑布流 + 下拉刷新 + 上拉加载更多

    一.思路分析和效果图 用vue来实现一个瀑布流效果,加载网络图片,同时有下拉刷新和上拉加载更多功能效果.然后针对这几个效果的实现,捋下思路: 根据加载数据的顺序,依次追加标签展示效果: 选择哪种方式实 ...

  8. 256位AES加密和解密

    /// <summary> /// 256位AES加密 /// </summary> /// <param name="toEncrypt">& ...

  9. 出现An App ID with Identifier 'com.XXX.XXX’ is not available. Please enter a different string.

    解决方法: 1.移除钥匙串中的开发证书,重新导入, 完全关闭Xcode; 2.再次打开Xcode,通过 Preferences - View Details - download 新的证书: 3.选择 ...

  10. Java入门 - 语言基础 - 18.正则表达式

    原文地址:http://www.work100.net/training/java-regular-expression.html 更多教程:光束云 - 免费课程 正则表达式 序号 文内章节 视频 1 ...