码迷,mamicode.com
首页 > 编程语言 > 详细

C++primer 练习11.33:实现你自己版本的单词转换程序

时间:2016-04-05 19:15:56      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

// 11_33.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<map>
#include<sstream>
#include<fstream>
using namespace std;
//用map文件来建立一个要转换的字符串对应的转换成的字符串
map<string, string> buildMap(ifstream &map_file)
{
    map<string, string> ma;
    string word;
    string convert;
    while (map_file >> word&&getline(map_file, convert))
    {
        ma[word] = convert.substr(1);
    }
    return ma;
}

//返回一个字符串在map中对应的字符串,如果没有,则返回字符串本身
const string& transform(const string &s, const map<string, string> &m)
{
    auto ite = m.find(s);
    if (ite != m.end())
        return ite->second;
    else return s;
}

//单词转换函数,输入一个转换文件,和一个输入文件,打印转换后的文本
void word_transform(ifstream &map_file, ifstream &input)
{
    map<string, string> ma = buildMap(map_file);
    string line, word;
    while (getline(input,line))
    {
        istringstream is(line);
        bool wordtag = true;
        while (is >> word)
        {
            if (wordtag)
                wordtag = false;
            else cout << " ";
            cout << transform(word, ma);
        }
        cout << endl;
    }
}

int main()
{
    word_transform(ifstream("D:\\file\\11.3.6_map.txt"), ifstream("D:\\file\\11.3.6_input.txt"));
    return 0;
}

 

C++primer 练习11.33:实现你自己版本的单词转换程序

标签:

原文地址:http://www.cnblogs.com/csudanli/p/5356232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!