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