map实现单词转换程序的例子
代码来源于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实现单词转换程序的例子的更多相关文章
- 自定义实现InputFormat、OutputFormat、输出到多个文件目录中去、hadoop1.x api写单词计数的例子、运行时接收命令行参数,代码例子
一:自定义实现InputFormat *数据源来自于内存 *1.InputFormat是用于处理各种数据源的,下面是实现InputFormat,数据源是来自于内存. *1.1 在程序的job.setI ...
- map集合修改其中元素 去除Map集合中所有具有相同值的元素 Properties长久保存的流操作 两种用map记录单词或字母个数的方法
package com.swift.lianxi; import java.util.HashMap; import java.util.Iterator; import java.util.Map; ...
- 关于MapReduce单词统计的例子:
要统计的文件的文件名为hello hello中的内容如下 hello you hello me 通过MapReduce程序统计出文件中的各个单词出现了几次.(两个单词之间通过tab键进行的分割) im ...
- C++primer 练习11.33:实现你自己版本的单词转换程序
// 11_33.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- poj1002 字典树+map+查询单词出现次数
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 309235 Accepted: 55223 Descr ...
- 字符拆分存入Map计算单词的个数
///计算从命令行输入单词的种类与个数//Map<key,Value>Key-->单词:Value-->数量
- Android List<Map<String,String>转json(例子)
package com.armslee.json.test.cases; import java.util.ArrayList; import java.util.HashMap; import ja ...
- Storm官方提供的trident单词计数的例子
上代码: public class TridentWordCount { public static class Split extends BaseFunction { @Override publ ...
- PTA1071 - Speech Patterns - map计算不同单词个数
题意 输出给定字符串出现最多的字符串(小写输出)和出现次数. 所求字符串要求:字符中可以含有A-Z.0-9. 比如说题目给出的Can1,我们可以转换成can1,can1就算一个字符串整体,而不是单独的 ...
随机推荐
- DOCKER学习_007:Docker的套接字介绍
根据https://www.cnblogs.com/zyxnhr/p/11825331.html这个文章,已经可以正常安装一个docker服务 查看Docker状态 [root@docker-serv ...
- CentOS 下 jenkins 安装
前置条件 jdk 和 maven 都配置好的环境,不赘述. 下载安装文件 选择一个 rpm 包 http://pkg.jenkins-ci.org/redhat/ 完成后执行命令 sudo rpm - ...
- Java函数式接口与Lambda表达式
什么是函数式接口? 函数式接口是一种特殊的接口,接口中只有一个抽象方法. 函数式接口与Lambda表达式有什么关系? 当需要一个函数式接口的对象时,可以提供一个lambda表达式. package l ...
- 「洛谷P1198」 [JSOI2008]最大数 解题报告
P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制: ...
- 【一起学源码-微服务】Ribbon 源码三:Ribbon与Eureka整合原理分析
前言 前情回顾 上一篇讲了Ribbon的初始化过程,从LoadBalancerAutoConfiguration 到RibbonAutoConfiguration 再到RibbonClientConf ...
- docker-tmpfs挂载
使用tmpfs挂载 卷和绑定装置允许在主机和容器之间共享文件,以便即使在容器停止后也可以保留数据. 如果你在Linux上运行Docker,你有第三个选择:tmpfs mounts.使用tmpfs装载创 ...
- docker-覆盖网络
docker network rm docker_gwbridge Error response from daemon: Error response from daemon: network ne ...
- 15.Python文本转化语音方法
1.用pywin32模块来将文本转化为语音 通过pip install pywin32安装模块,pywin32是个万金油的模块,太多的场景使用到它,但在文本转语音上,它却是个青铜玩家,简单无脑但效果不 ...
- Spring Cloud Stream消息驱动@SendTo和消息降级
参考程序员DD大佬的文章,自己新建demo学习学习,由于需要消息回执,看到了@SendTo这个注解能够实现,下面开始学习demo,新建两个项目cloud-stream-consumer消费端 和 cl ...
- 斯坦福算法分析和设计_2. 排序算法MergeSort
Motivate MergeSort是个相对古老的算法了,为什么现在我们还要讨论这么古老的东西呢?有几个原因: 它虽然年龄很大了,但是在实践中一直被沿用,仍然是很多程序库中的标准算法之一. 实现它 ...