[C++]换词程序

#pragma once
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <initializer_list>
#include <algorithm>
#include <unordered_set>
#include <numeric>
#include <unordered_map>
#include <stack>
#include <math.h>
#include <map>
#include <list>
#include <set>
#include <unordered_set>
#include <sstream>
#include <functional>
#include <iomanip>
#include <iterator>
#include <fstream>
//#include <time.h>
#include <ctime>
#include <cstdlib>
#include <deque>
map<string, string> buildMap(ifstream &map_file) {
    map<string, string> trans_map;
    string key;
    string value;
    while (map_file>>key&&getline(map_file,value))
    {
        if (value.size() > 1)
            trans_map.insert({ key,value.substr(1) });
        //    trans_map[key] = value.substr(1);
        else
            throw runtime_error("no rule for" + key);
        
    }
    return trans_map;
}
const string& transform(const string &s, const map<string, string> &m) {
    auto map_it = m.find(s);
    if (map_it != m.cend()) {
        return map_it->second;
    }
    else
        return s;

}
void word_transform(ifstream &map_file, ifstream &input) {
    auto trans_map = buildMap(map_file);
    string text;
    while (getline(input, text))
    {
        istringstream stream(text);
        string word;
        bool firstword = true;
        while (stream>>word)
        {
            if (firstword)
                firstword = false;
            else
                cout << " ";
            cout << transform(word, trans_map);
        }
        cout << endl;
    }
}
int main()
{
    ifstream f1("map.txt");
    ifstream f2("trans.txt");
    word_transform(f1, f2);
    return 0;
}
原文地址:https://www.cnblogs.com/lightmonster/p/11504706.html