C + + Self-study Note _ Word Conversion Map Object _ C + + Primer

Source: Internet
Author: User

Today in the "C + + Primer" in the 10th chapter of the time seems to meet a little bottleneck, turn back to the 8th chapter to eat a meal back grass. So, the old saying is good: owe always have to drop:)

A small program, very simple: Word Converter : The program input two files, the first file contains several pairs of words, the first word will appear in the input string, and the second word

is used for the output. Essentially, this file provides a collection of word conversions--which should be replaced with a second word when the first word is encountered. The second file provides the text that you want to convert.

For example: if the contents of the Word conversion file are:

and the text to be converted is:

Code:

#include <iostream>#include<map>#include<fstream>using namespacestd;intMain () {stringFileName1 ("F:\\file1.txt");//file1    stringFileName2 ("F:\\file2.txt");//file2    stringFileName3 ("F:\\file3.txt");//File3map<string,string> trans_map;//define the Map object, storing the contents of the File1    stringKey,value;//Key-value pairsIfstream infile;//infile provides read file operation, read File1.txtInfile.open (Filename1.c_str ());//don't forget to convert the file name to a C-style string!     if(!infile) {Cerr<<"error:enable Open File:"<<fileName1<<Endl; return 0; }Else{cout<<"Open File:"<<fileName1<<"Success"<<Endl; }     while(Infile>>key>>value) {//reads the key value pairs in the file1 and stores them inside the Trans_mapTrans_map.insert (Make_pair (Key,value));    } infile.close ();                           Ifstream input; //input provides read file operation, read File2.txtOfstream outfile;//outfile provides write file operation, write File3.txtInput.open (Filename2.c_str ()); if(!input) {Cerr<<"error:enable Open File:"<<fileName2<<Endl; return 0; }Else{cout<<"Open File:"<<fileName2<<"Success"<<Endl;    } outfile.open (Filename3.c_str ()); if(!outfile) {Cerr<<"error:enable Open File:"<<fileName3<<Endl; return 0; }Else{cout<<"Open File:"<<fileName3<<"Success"<<Endl; }    stringWord; BOOLfirstword=true;//The output that is used to determine whether the first word is used to process spaces     while(Input>>word) {//read a Word (string)map<string,string>::const_iterator Iter=trans_map.find (word);//If Word is present, returns an iterator that points to word        if(iter!=Trans_map.end ()) {Word=iter->second;//an iterator is a pointer to an existing element that changes Word to the corresponding word in the file1.txt rulecout<<word<<endl;//This is the output that was played on the console.        }        if(FirstWord) {//the case of a 1th wordfirstword=false; OutFile<<Word; }        Else{outfile<<" "<<word;//from the beginning of the 2nd, a space will be printed in front of each word.} input.clear ();    Outfile.clear (); } outfile<<Endl; return 0;}

Robustness is debatable and needs to be optimized several times.

The program will write the result in a text file:

Take a nap:)

C + + Self-study Note _ Word Conversion Map Object _ C + + Primer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.