Map implementation of the Word conversion program example

Source: Internet
Author: User

code from C + + primer 10.3

Function: Known as a one by one corresponding dictionary, to find a small document corresponding to the "translation"

The dictionary is as follows:

A A b b c c D d e E

Input:

D d E

Code:

Requires two files, one is a dictionary file, one is input file # include <iostream> #include <fstream> #include <sstream> #include < utility> #include <map> #include <string>using namespace std;ifstream& open_file (Ifstream &in,        Const string &file) {in.close (); In.clear (); In.open (File.c_str ()); return in; }int Main (int argc,char * * argv) {map<string, string> trans_map;string key, value;if (argc! = 3) {Throw Runtime_error ( "Wrong number of arguments, we need an dictionary.txt and an Input.txt");} Ifstream map_file;if (!open_file (map_file,argv[1])) {throw Runtime_error ("No dictionary File");} while (Map_file >> key >> value) {Trans_map.insert (Make_pair (key, value));} Ifstream input;if (!open_file (input, argv[2]) {throw runtime_error ("No input File");} String Line;while (getline (input, line)) {Istringstream stream; string Word;bool FirstWord = True;while (Stream > > Word) {map<string, String>::const_iterator map_it = Trans_map.find (word); if (map_it! =Trans_map.end ()) {word = Map_it->second;} if (FirstWord) {FirstWord = false;} Else{cout << "";} cout << Word;} cout << Endl;} return 0;}

Operation, Makefile:

edit:trans_words.og++-O Edit trans_words.otrans_words.o:trans_words.cppg++-C trans_words.cppclean:rm TRANS_WORDS.O

run.sh

#!/bin/shmake./edit Dictionary.txt Input.txt

Results:

D d E

Map implementation of the Word conversion program example

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.