这篇代码有几个知识点可以复习一下,而且小白学到了新知识o(╯□╰)o
 #include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <sstream>
using namespace std; ifstream& open_file(ifstream&,const string&);
/*two input main parameter of main:
*The first is name of the word transformation file
*The second is name of the input to transform
*/
int main(int argc, char **argv )
{
/*Use to hold the pair of word and trans_word:
Key is the word to look for in the input, Value is the word to use in the output
*/
map<string, string> trans_map;
string key, value;
if(argc != )
throw runtime_error("Wrong number of arguments!");
ifstream map_file;
if (!open_file(map_file, argv[]))
{
throw runtime_error("No transformation file!");
}
//read the transformation map and build the map
while (map_file >>key >>value)
trans_map.insert(make_pair(key, value)); ifstream input;
if(!open_file(input, argv[]))
throw runtime_error("No input file!");
string line; //hold each line from the input //read the text to transform it a line at a time
while ( getline(input, line)) //read a line from the input to line
{
istringstream stream(line);
string word;
bool firstword = true;
while (stream >>word)
{
/*Use word find the key of trans_map*/
map<string, string>::const_iterator map_it = trans_map.find(word);
if (map_it !=trans_map.end())
/*if there is substitute for word, change the word*/
word = map_it->second;
if (firstword)
firstword = false;
else
cout << " ";
cout << word;
}
cout << endl;
}
return ;
} ifstream& open_file(ifstream &in, const string &file )
{
in.close();
in.clear();
in.open ( file.c_str() );//open the file we were given
return in;
}

首先理清楚单词转换的流程:

1.明确输入文件有两个:
  ①mapfile,单词转换集合,其第一个值为待转换单词,第二个值为转换后单词
  ②infile,输入文件
2.明确对文件的操作
  ①对于mapfile,如果打开正确的话,读入单词转换对,将其转化为:第一个值为key,第二个值为value;调用insert在容器中插入新元素,循环执行。
  ②对于infile,使用getline函数逐行读取文件,将文件的每个单词在map_file中查找,若找到则用map_file中的value将其替代,最后输出。注:输出空格的细节! 

知识点总结:

1.main函数的形参

main(int argc, char **argv);

argc是一个整型变量,指的是命令行输入参数的个数,argv是字符串数组,它包含argc个字符串,每个字符串存储着一个命令行参数,其也可以写作char *argv[]。

如该例子argv[0]存储的是命令名,argv[1],argv[2]存储着执行该程序所需要的两个文件名参数。

argc和argv就是一个名字,可以改变的,如写成arc和arv,丝毫不影响。

对形参个数的检查:

if(argc != )
  throw runtime_error("Wrong number of arguments!");

2.文件打开

ifstream& open_file(ifstream &in, const string &file )
{
in.close();
in.clear();
in.open ( file.c_str() );//open the file we were given
return in;
}

输入的两个参数:文件流和文件名

最后要么将in和指定文件绑在了一起,要么处于错误条件状态。

错误使用throw runtime_error

3.文件读取

对于文件1,将输入的文件中的单词转换对使用make_pair创建pair对象trans_map。

再用insert函数,如果key不存在,则添加。

while (map_file >>key >>value)
trans_map.insert(make_pair(key, value));

对于文件2,首先使用getline逐行读入,istringstream对象可以绑定一行字符串,然后以空格为分隔符把该行分隔开来。

(例:

stringstream strm(s); //创建存储s的副本的 stringstream 对象,其中 s 是 string 类型的对象

    while ( getline(input, line)) //read a line from the input to line
  {
istringstream stream(line);
string word;
bool firstword = true;
while (stream >>word)
{
/*Use word find the key of trans_map*/
map<string, string>::const_iterator map_it = trans_map.find(word);
if (map_it !=trans_map.end())
/*if there is substitute for word, change the word*/
word = map_it->second;
if (firstword)
firstword = false;
else
cout << " ";
cout << word;
}
cout << endl;
}

从输入文件中一个个读入单词word,如果能在trans_map的key中找到,则将word更改为trans_map的value值,循环录入,使用迭代器:

map<string, string>::const_iterator  map_it = trans_map.find(word);

最后输出word。

答案中还考虑了空格的录入,但是为什么不直接输出cout << word<<" "; ??

if (firstword)
firstword = false;
else
cout << " ";

4.技术小白的一点收获

知道了怎么向main函数传递参数:

通过 项目---属性---配置属性---调试---命令参数惊醒main函数参数的添加,各个参数之间以空格隔开;注意此时添加进去的参数被依次保存为argv[1]、argv[2]……

argc也相应进行的增加,其中第一个参数argv[0]是编译器自动传入的生成的.exe所在的路径。之后在main函数中可以通过printf把已经传入的参数个数和相应的参数内容打印出来,此时的参数类型都是字符串类型的,如果需要要进行相应的类型转化。

当然还有Dos的方法,

“运行”->cmd->d:\e\f.exe g1 g2 g3......

不过为嘛不可以???待解?

对C++ Primer的10.3.9单词转换的思考的更多相关文章

  1. [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 ...

  2. C++ Primer : 第十一章 : 关联容器示例: 一个单词转换的map

    单词转换就是:将一些缩写的单词转换为实际的文本.第一个文件保存的是转换的规则,而第二个文件保存的是要转换的文本. 假设单词转换的规则的文件如下: brb be right back k okay? y ...

  3. 最短路径算法-Dijkstra算法的应用之单词转换(词梯问题)(转)

    一,问题描述 在英文单词表中,有一些单词非常相似,它们可以通过只变换一个字符而得到另一个单词.比如:hive-->five:wine-->line:line-->nine:nine- ...

  4. oracle转Mysql中,varchar2(10)和number应该转换为什么类型?

    一. varchar2(10)和number应该转换为什么类型? oracle转成mysql时:varchar2(10)可以转成varchar(10)number则要看oracle中存储的具体是什么类 ...

  5. [STL]单词转换

    如果单词转换文件的内容是: 'em         themcuz         becausegratz      grateful i             Inah        nopos ...

  6. oracle转Mysql中,varchar2(10)和number应该转换为什么类型? (转)

    一. varchar2(10)和number应该转换为什么类型? oracle转成mysql时:varchar2(10)可以转成varchar(10)number则要看oracle中存储的具体是什么类 ...

  7. 单词转换成向量形式 word2vec

    word2vec(word to vector)是一个将单词转换成向量形式的工具.可以把对文本内容的处理简化为向量空间中的向量运算,计算出向量空间上的相似度,来表示文本语义上的相 似度.word2ve ...

  8. [C++ Primer] : 第10章: 泛型算法

    概述 泛型算法: 称它们为"算法", 是因为它们实现了一些经典算法的公共接口, 如搜索和排序; 称它们是"泛型的", 是因为它们可以用于不同类型的元素和多种容器 ...

  9. C++primer 练习10.16

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

随机推荐

  1. CM5(Cloudera Manager 5) + CDH5(Cloudera's Distribution Including Apache Hadoop 5)的安装详细文档

    参考 :http://www.aboutyun.com/thread-9219-1-1.html Cloudera Manager5及CDH5在线(cloudera-manager-installer ...

  2. 巧用MySQL之Explain进行数据库优化

    前记:很多东西看似简单,那是因为你并未真正了解它. Explain命令用于查看执行效果.虽然这个命令只能搭配select类型语句使用,如果你想查看update,delete类型语句中的索引效果,也不是 ...

  3. python 多行字符串

    字符串是要用引号(双引号,单引号,多行用三个引号)引起来 TypeError: not enough arguments for format string you have to specify m ...

  4. freemaker分页模板

    <link href="${base}/res/pra/css/style.css" rel="stylesheet" type="text/c ...

  5. error C2589: “(”: “::”右边的非法标记 error C2059: 语法错误 : “::

    1. 错误输出 ./zlibrary/ui/src/win32/w32widgets/W32VBorderBox.cpp(114) : error C2589: “(”: “::”右边的非法标记    ...

  6. Delphi- 数据加密和解密

    Delphi进行数据加密,在数据库方面经常要使用到.从网上转载过来的,以后会经常会用到. 一.MD5加密算法 在C#/.Net里提供了MD5加密的类库.在Delphi中没有.只能自己建一个新的单位,将 ...

  7. 《Spring技术内幕》学习笔记17——Spring HTTP调用器实现远程调用

    1.Spring中,HTTPInvoker(HTTP调用器)是通过基于HTTP协议的分布式远程调用解决方案,和java RMI一样,HTTP调用器也需要使用java的对象序列化机制完成客户端和服务器端 ...

  8. AptanaStudio3 安装在win7 64bit时遇到的问题

    最近在研究前端语言,想起可以使用AptanaStudio这个前端利器,没想到安装时却遇到波折.先从网上下载了Aptana版本 3.6.0 64bit问题1 安装进度缓慢,第一次安装时,显示downlo ...

  9. Android实时监听网络状态(2)

    在开发android应用时,涉及到要进行网络访问,时常需要进行网络状态的检查,以提供给用户必要的提醒.一般可以通过ConnectivityManager来完成该工作. ConnectivityMana ...

  10. linux下启动某个进程

    在关闭窗口的情况下,能够在后台继续运行,如 启动命令 node /home/node_modules/pixel-ping/lib/pixel-ping.js /home/node_modules/p ...