程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> c++ 單詞轉換 map對象

c++ 單詞轉換 map對象

編輯:C++入門知識

c++ 單詞轉換 map對象


#include 
#include #include #include #include #include using namespace std; ifstream& openfile(ifstream &in,const string &filename){ in.close();//close in case it was alrady open in.clear();//clear any existing errors in.open(filename.c_str());//open the file we were given //in要麼於指定文件綁定起來了,要麼處於錯誤條件狀態 return in;//condition state is good if open succeeded } int main(int argc,char** argv) { map 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 map while(map_file>>key>>value){ trans_map.insert(pair(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 time while(getline(input,line)){ istringstream stream(line); //read the line a word at a time string word; //讀字符串流 bool lineFirst=true;//controls whether a space is printed while(stream>>word){ //ok:the actual mapwork,this part is the heart of the program map::const_iterator iter=trans_map.find(word); if(iter!=trans_map.end()){ //replace it by the transformation value in the map word=iter->second; } if(lineFirst){ cout<

transform.txt文件內容:

 

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

source.txt文件內容:

 

 

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

 

程序運行輸出:

\

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved