C++primer学习:关联容器练习(4)

单词转换程序:将一个文本中的单词按照另一个文本的转换规则,替换成另外一个文本中的单词.并且开头字母大写;

#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个或者多个元素.并且提供了一系列桶的接口与管理操作.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值