c++ primer 10.3.9

來源:互聯網
上載者:User

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
using namespace::std;

int main()
{
    string f1("D://1.txt"),f2("D://2.txt"),f3("D://3.txt");
    map<string,string> map_trans;
    string key,value;
    ifstream trans_file(f1.c_str());
    if(!trans_file)
    cerr<<"No translation file!"<<endl;
    while(trans_file>>key>>value)
    map_trans.insert(make_pair(key,value));
    // Read the translation file and build map
    ifstream input_file(f2.c_str());
    if(!input_file)
    cerr<<"No input file!"<<endl;
    ofstream output_file(f3.c_str());
    if(!output_file)
    cerr<<"No Output file!"<<endl;
    string line;
    while(getline(input_file,line))
    {
     istringstream stream(line);
     string word;
     bool firstword=true;             
     while(stream>>word)
     {
     map<string,string>::const_iterator map_it=map_trans.find(word);
     if(map_it!=map_trans.end())
     word=map_it->second;
     if(firstword)
     firstword=false;
     else output_file<<" ";
     output_file<<word;
                        }
     output_file<<endl;               
    }
    system("pause");
    return 0;  
    }

 

1.txt 內容初始化

'em them cuz because grantz grateful i I nah no pos supposed sez said tanx thanks wuz was

 

2.txt 內容初始化

nah i sez tanx cuz i wuz pos to not cuz i wuz grantz

 

3.txt 裡面將輸出

no I said thanks because I was supposed to not because I was grateful

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.