zoukankan      html  css  js  c++  java
  • [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;
    }
  • 相关阅读:
    Python Web学习笔记之Python多线程基础
    Python入门之python可变对象与不可变对象
    Python Web学习笔记之SOCK_STREAM和SOCK_DGRAM
    background和background-position相关笔记
    自定义switch开关
    获取浏览器类型和版本号
    随机生成字符串
    white-space详解
    文件选择按钮随笔
    mouse的各种事件
  • 原文地址:https://www.cnblogs.com/lightmonster/p/11504706.html
Copyright © 2011-2022 走看看