C++primer学习:关联容器练习(4)
2015-10-10 23:14
302 查看
单词转换程序:将一个文本中的单词按照另一个文本的转换规则,替换成另外一个文本中的单词.并且开头字母大写;
#include "iostream" #include "vector" #include "list" #include "map" #include "set" #include "string" #include "algorithm" #include "utility" #include "fstream" #include "sstream" using namespace std; map<string,string> build_map(ifstream&map_file) { map<string, string> Trans; string line, word; while (map_file>>word&&getline(map_file,line)) { Trans[word] = line.substr(1);//去掉空格 } return Trans; } string convert(const map<string,string>&Trans_map,const string &word) { auto pos = Trans_map.find(word); return (pos == Trans_map.cend() ? word : pos->second); } void word_transform(string filename1,string filename2) { ifstream map_file(filename1), input_file(filename2); auto trans_map = build_map(map_file); string word, line; while (getline(input_file,line))//获取输入内容 { istringstream is(line); string line2; while (is >> word)//读入输入的单词 line2 += convert(trans_map, word)+" "; if (!(ispunct(line2[0]) || isdigit(line2[0]))) line2[0] = toupper(line2[0]); cout <<line2<< endl; } } int main() { word_transform("Text1.txt", "Text2.txt"); return 0; }
输入:
where r you
y dont u send me a pic
k thk 18r
“I am the king of wor
转换规则:
k okay
y why
r are
u you
pic picture
thk thanks!
18r later
===================================================================
无序关联容器,不需要维护容器的序.使用==来比较函数,hash函数来生成每个对象的key值.它在存储上组织为一组桶,桶里面管理着0个或者多个元素.并且提供了一系列桶的接口与管理操作.
相关文章推荐
- C++STL之string上
- 转载:读懂C/C++递归
- hdoj 2030 汉字统计 (汉字机内码特点) C++
- C语言的随机数
- C语言 用链表实现电话本的功能
- C语言 用顺序表实现电话本的功能
- C++ 中stringstream 的作用
- 带你玩转Visual Studio——带你理解微软的预编译头技术
- c++ primer学习笔记6_标准库string
- 各种排序算法的分析与实现(C++版)
- c++ primer学习笔记5_复合类型续2
- c++类与c struct的区别
- hdoj 2029 Palindromes _easy version C++
- 黑马程序员--C语言之指针<一>
- hdoj 2028 Lowest Common Multiple Plus C++
- 基于Gsoap 的ONVIF C++ 库
- c/c++程序存储区
- C++primer学习:关联容器(3)
- B树算法与实现 (C语言实现)
- 重学C++Primer笔记6---指针、typedef、const、define彻底理解