代码来源于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. 网络OSI七层架构与TCP四层架构的应用与区别

    1.OSI七层网络模型介绍 OSI(Open System Interconnection,开放系统互连)七层网络模型称为开放式系统互联参考模型 ,是一个逻辑上的定义,一个规范,它把网络从逻辑上分为了 ...

  2. Git是什么?

    Git是什么? Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控 ...

  3. VC++取MD5算法记录下以后用得到(转)

    这个是网上扒下来的 作者已经无法知道是谁了 MD5.h #ifndef MD5_H #define MD5_H #include <string> #include <fstream ...

  4. Django 链接MySQL及数据操作

    Django 链接MySQL Django创建的项目自带的数据库是SQLite3,我们想要链接MySQL的话,需要更改settings.py中的配置 1.在MySQL中创建好数据库,Django项目不 ...

  5. 洛谷P1189 SEARCH 题解 迭代加深

    题目链接:https://www.luogu.com.cn/problem/P1189 题目大意: 给你一个 \(n \times m\) 的矩阵,其中有一些格子可以走,一些各自不能走,然后有一个点是 ...

  6. web(www)服务器搭建Redhat5.4

    WWW服务概念及服务原理 目前,在Internet上最热门的服务之一就是WWW (World Wide Web)菔务,til^^Web服务.通过WWW触务,岢在Internet 或企业内部网络中传播. ...

  7. my_mysql

    ###一键偷懒YUM安装MySQbL### 1.安装mysql数据库 #yum install -y mariadb-server  mariadb 2.登录mysql数据库常用选项 -h:指定服务端 ...

  8. C语言联合体(union)的使用方法及其本质-union

    转载自:https://blog.csdn.net/si_zhou_qun_84342712/article/details/53187106 1.联合体union的基本特性——和struct的同与不 ...

  9. GC 为什么要挂起用户线程? 什么愁什么怨?

    GC 为什么要挂起用户线程? 什么愁什么怨? 前言 JVM 系列文章的第一篇.敬请期待后续. 故障描述 某年某月某日 上午,线上发生故障,经过排查,发现某核心服务 Dubbo 接口超时. 故障根源 查 ...

  10. 基于Saltstack、Artifactory打造传统模式下持续部署平台

    一.持续部署 1. 现状 由于没有建立标准的持续部署流程,导致了版本管理混乱,制品管理混乱,上线持续时间长,上线测试覆盖不全面,业务流量上升后故障较多,排查复杂.运维.测试.开发人员每次版本迭代的时候 ...