C + + Word conversion Map Object

Source: Internet
Author: User

#include <map> #include <sstream> #include <fstream> #include <iostream> #include <string > #include <exception>using namespace std;ifstream& openfile (ifstream &in,const string &filename) {in.close ();//close in case it is alrady openin.clear ();//clear any existing Errorsin.open (FILENAME.C_STR ());//open the The file we were Given//in is either bound to the specified files, or is in an error condition state of return in;//condition is good if open succeeded}int main (int argc,char* * argv) {map<string,string> trans_map;string key,value;ifstream map_file;if (!openfile (Map_file, "transform.txt ") {throw Runtime_error (" No Transformation File ");} Read the Tansformation map and build the Mapwhile (map_file>>key>>value) {Trans_map.insert (pair<string ,string> (Key,value));} Ifstream input;if (!openfile (Input, "Source.txt")) {throw runtime_error ("No input File");} String Line;//hold each line from the Input//read the text to transform it a line at a timewhile (Getline (input,line)) {ISTR Ingstream StReam (line); Read the line a word at a timestring word;//reads the string stream bool Linefirst=true;//controls whether a space is printedwhile (STREAM&G T;>word) {//ok:the actual mapwork,this part is the heart of the Programmap<string,string>::const_iterator iter= Trans_map.find (Word), if (Iter!=trans_map.end ()) {//replace It by the transformation value in the Mapword=iter->second ;} if (Linefirst) {cout<<word;linefirst=false;} else{cout<< "" <<word;//print space between words;} Cout<<endl;//done with the line of Input}return 0;}

Transform.txt File Contents:

EM themcuz Becausegratz gratefuli inah Nopossupposedsez said Tanx Thankswuz was

Source.txt File Contents:

Nah i sez tanx cuz i wuz pos tonot cuz i wuz Gratz

Program Run output:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Word conversion Map Object

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.