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就算一个字符串整体,而不是单独的 ...
随机推荐
- 用Tasker实现收到Android手机短信自动转发到邮箱
发送短信到邮箱的原理与 <用Tasker实现收到Android手机短信自动转发到邮箱>有些类似. 发送短信到邮箱是利用Ifttt这个服务将短信转发到邮箱中.Ifttt服务的可扩展性很强, ...
- lombok工作原理分析
在Lombok使用的过程中,只需要添加相应的注解,无需再为此写任何代码.但是自动生成的代码到底是如何产生的呢? 核心之处就是对于注解的解析上.JDK5引入了注解的同时,也提供了两种解析方式. 运行时解 ...
- $POJ1742\ Coins$ 多重背包+贪心
Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最 ...
- 爬虫 -- JS调试
开发者工具(F12) 其中常用的有Elements(元素面板).Console(控制台面板).Sources(源代码面板).Network(网络面板) 找 JS 文件的几种方法 1.找发起地址 2.设 ...
- 1051 复数乘法 (15 分)C语言
复数可以写成 (A+Bi) 的常规形式,其中 A 是实部,B 是虚部,i 是虚数单位,满足 i^2=−1:也可以写成极坐标下的指数形式 (R×e(Pi) ),其中 R 是复数模,P 是辐角,i ...
- CF749D Leaving Auction set排序查找
CodeForces 749D. Leaving Auction 传送门 There are n people taking part in auction today. The rules of a ...
- 生产环境中使用docker注意点
是否对容器使用的资源进行合理限制,比如内存 CPU 网络带宽等. 是否设置合理的网络访问限制,如 非root用户 iptables. 是否有在docker无法使用时的备选方案,如提供非docker环境 ...
- pinpoint实现告警推送至钉钉和微信群
前言 在前面的文章中,我们学习了如何通过java实现将消息发送到钉钉.和将消息发送到微信群聊. 基于上述基础,我们今天来接入pinpoint的告警,发送到钉钉群. 实操前准备 开始之前,推荐阅读一下, ...
- 重拾c++第三天(5):循环和关系表达式
1.改变步长 ;i<;i=i+) 2.*与++优先级相同,从右向左 3.strcmp函数比较两个字符串,=0两个相同,><0 ----> str1><str2 4. ...
- 头条面试竟然问我maven
maven package和maven install 有什么区别? 你常用的maven命令有哪些? <dependencyManagement> 是干什么的? 还有用过其它构建工具吗? ...