[STL]单词转换
如果单词转换文件的内容是:
'em them
cuz because
gratz grateful
i I
nah no
pos supposed
sez said
tanx thanks
wuz was
而要转换的文本是:
nah i sez tanx cuz i wuz pos to
not cuz i wuz gratz
则程序将产生如下输出结果:

代码如下:
#include<iostream>
#include<map>
#include<string>
#include<fstream>
#include<sstream>
using namespace std; int main()
{
map<string,string> trans_map;
string key,value;
ifstream ifile("a.txt");//a.txt存放转换文件
if(!ifile)
throw runtime_error("no translation file");
while(ifile>>key>>value)
{
trans_map.insert(make_pair(key,value));
}
ifstream input("b.txt");//b.txt存放要转换的文件
if(!input)
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())//若map中存在就替换
{
word=map_it->second;
}
if(firstword)
{
firstword=false;
}
else
{
cout<<" ";
}
cout<<word;
}
cout<<endl;
}
return ;
}
[STL]单词转换的更多相关文章
- [CareerCup] 18.10 Word Transform 单词转换
18.10 Given two words of equal length that are in a dictionary, write a method to transform one word ...
- C++ Primer : 第十一章 : 关联容器示例: 一个单词转换的map
单词转换就是:将一些缩写的单词转换为实际的文本.第一个文件保存的是转换的规则,而第二个文件保存的是要转换的文本. 假设单词转换的规则的文件如下: brb be right back k okay? y ...
- 对C++ Primer的10.3.9单词转换的思考
这篇代码有几个知识点可以复习一下,而且小白学到了新知识o(╯□╰)o #include <iostream> #include <string> #include <ma ...
- 最短路径算法-Dijkstra算法的应用之单词转换(词梯问题)(转)
一,问题描述 在英文单词表中,有一些单词非常相似,它们可以通过只变换一个字符而得到另一个单词.比如:hive-->five:wine-->line:line-->nine:nine- ...
- 单词转换成向量形式 word2vec
word2vec(word to vector)是一个将单词转换成向量形式的工具.可以把对文本内容的处理简化为向量空间中的向量运算,计算出向量空间上的相似度,来表示文本语义上的相 似度.word2ve ...
- c++学习笔记——个单词转换的map程序详解
实现功能:给定一个string,将它转换为另一个string.程序输入是两个文件,第一个文件保存转换规则,第二个文件为将要进行转换的文本. IDE:Windows7+VS2013 #include & ...
- leetcode 面试题 17.22. 单词转换(DFS+回溯)
题目描述 思路分析 这题回溯,先想出它的空间解是什么,这里空间解,其实就是给的原字符串到结束字符串中间的变形过程,那么就可以容易的画出一个解空间树,用深度搜索进行搜索, 剪枝后,进入下一个维度,再进行 ...
- UVA - 10815 - Andy's First Dictionary STL
Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him ...
- C++ 各种基本类型间的转换
常用的转换方法: 流转换 STL标准函数库中函数转换 流转换 流转换主要是用到了<sstream>库中的stringstream类. 通过stringstream可以完成基本类型间的转换, ...
随机推荐
- OpenGL第18,19,20讲小结
18讲是通过调用库函数画一些简单的二次几何体,比如球体.圆锥体.圆盘等等. 19讲简单的讲了下粒子系统.其实就是三角形贴上星星的纹理,通过启用混合(GL_BLEND)来达到一种动态的粒子效果.通过修改 ...
- ZigBee安全相关
ZigBee安全由AES加密算法和CCM操作方式作为安全方案,广泛使用在ZigBee联盟的通信协议中.ZDO层负责安全策略和安全配置的管理. Technorati 标签: ZigBee 安全 2. 配 ...
- ant条件逻辑
<condition property="sdk-folder" value="E:\android\android-sdk\adt-bundle-windows- ...
- 《搭建DNS内外网的解析服务》RHEL6
首先说下: 搭建的这个dns内外网的解析,是正向解析,反向解析自己根据正向解析把文件颠倒下就ok了 第一步我们先搭建一个DNS的正反向解析(参考上篇DNS正反向解析,这是上篇做过的) 第二部才是搭建内 ...
- 用CSS实现Firefox 和IE 都支持的Alpha透明效果
有的时候,为了实现一些特殊效果,需要将页面元素变透明,本文介绍的就是用 CSS 实现 Firefox 和 IE 都支持的 Alpha 透明效果.CSS: filter:alpha(opacity=50 ...
- 转: js操作cookie
cookie的几个概念 http://dearhappyfish.blog.163.com/blog/static/1901094152012422114753777/ js操作cookie 转:ht ...
- Redis源码研究--启动过程
---------------------6月23日--------------------------- Redis启动入口即main函数在redis.c文件,伪代码如下: int main(int ...
- Spark 大数据平台
Apache Spark is an open source cluster computing system that aims to make data analytics fast - both ...
- 1103. Integer Factorization (30)
The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...
- URL地址下载图片到本地
package test.dao; import eh.base.dao.DoctorDAO; import eh.entity.base.Doctor; import junit.framework ...