欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用map實現(xiàn)單詞轉(zhuǎn)換的實例分析

 更新時間:2013年05月28日 15:38:28   作者:  
本篇文章是對使用map實現(xiàn)單詞轉(zhuǎn)換的代碼實例進(jìn)行了纖細(xì)的分析介紹,需要的朋友參考下
使用map實現(xiàn)單詞轉(zhuǎn)換的實例分析
從map中查找單詞時必須使用find函數(shù),不能使用下表,因為在map中使用下標(biāo)訪問不存在的元素將導(dǎo)致在map容器中添加一個新的元素,新元素的key即要查找的內(nèi)容。
復(fù)制代碼 代碼如下:

/*****************************************************************************
* Open file
*****************************************************************************/
ifstream& open_file(ifstream &in, const string &file)
{
 in.close();  // close in case it was already open
 in.clear();  // clear any existing errors
 // if the open fails, the stream will be in an invalid state
 in.open(file.c_str()); // open the file we were given
 return in; // condition state is good if open succeeded
}
/*****************************************************************************
* Word Transform
*****************************************************************************/
void WordTransform(const string rule, const string infile)
{
 if (rule.empty() || infile.empty())
 {
  return;
 }
 map<string ,string> trans_map;
 string key, value;
 // Open transformation file and check that open succeeded
 ifstream map_file;
 if (!open_file(map_file, rule))
 {
  throw runtime_error("No transformation file.");
 }
 // Read the transformation map and build the map
 while (map_file >> key >> value)
 {
  trans_map.insert(make_pair(key, value));
 }
 // Open the input file and check that the open succeeded
 ifstream input;
 if (!open_file(input, infile))
 {
  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 bFirstWordFlg = true; // Controls whether a space is printed
  while (stream >> word)
  {
   // ok: the actual mapwork, this part is the heart of the program
   map<string, string>::const_iterator map_it = trans_map.find(word);
   // If this word is in the transformation map
   if (map_it != trans_map.end())
   {
    // Replace it by the transformaion value in the map
    word = map_it->second;
   }
   if (bFirstWordFlg)
   {
    bFirstWordFlg = false;
   }
   else
   {
    cout << " "; // Print space between words
   }
   cout << word;
  }
  cout << endl; // Done with this line of input
 }
}

相關(guān)文章

  • C語言實現(xiàn)簡單的通訊錄管理系統(tǒng)

    C語言實現(xiàn)簡單的通訊錄管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C++初階之list的模擬實現(xiàn)過程詳解

    C++初階之list的模擬實現(xiàn)過程詳解

    在C++中我們經(jīng)常使用STL,那個在那些我們常用的數(shù)據(jù)結(jié)構(gòu)vector,list的背后,又是如何實現(xiàn)的呢?這篇文章主要給大家介紹了關(guān)于C++初階之list的模擬實現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • C++指針數(shù)組、數(shù)組指針、數(shù)組名及二維數(shù)組技巧匯總

    C++指針數(shù)組、數(shù)組指針、數(shù)組名及二維數(shù)組技巧匯總

    這篇文章主要介紹了C++指針數(shù)組、數(shù)組指針、數(shù)組名及二維數(shù)組技巧匯總,對于深入理解C++數(shù)組與指針來說非常重要,需要的朋友可以參考下
    2014-08-08
  • 淺談C++ 虛函數(shù)

    淺談C++ 虛函數(shù)

    這篇文章主要介紹了C++ 虛函數(shù)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下
    2020-09-09
  • C語言指針應(yīng)用簡單實例

    C語言指針應(yīng)用簡單實例

    這篇文章主要介紹了C語言指針應(yīng)用簡單實例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • C++ getcwd函數(shù)獲取項目運(yùn)行路徑方法詳解

    C++ getcwd函數(shù)獲取項目運(yùn)行路徑方法詳解

    在Linux下做QT項目時,需要獲取項目的運(yùn)行路徑,于是用getcwd函數(shù)進(jìn)行獲取,然后在Windows下進(jìn)行測試,發(fā)現(xiàn)獲取到的是程序的項目路徑,即代碼文件路徑,然后再Linux QT中測試,獲取到的又是運(yùn)行路徑,這就很納悶了。經(jīng)過再三測試,終于發(fā)現(xiàn)了原因
    2022-10-10
  • C++11模板元編程-std::enable_if示例詳解

    C++11模板元編程-std::enable_if示例詳解

    這篇文章主要給大家介紹了關(guān)于C++11模板元編程-std::enable_if的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • c++雙向鏈表操作示例(創(chuàng)建雙向鏈、雙向鏈表中查找數(shù)據(jù)、插入數(shù)據(jù)等)

    c++雙向鏈表操作示例(創(chuàng)建雙向鏈、雙向鏈表中查找數(shù)據(jù)、插入數(shù)據(jù)等)

    這篇文章主要介紹了c++雙向鏈表操作示例,包括創(chuàng)建雙向鏈、刪除雙向鏈表、雙向鏈表中查找數(shù)據(jù)、插入數(shù)據(jù)等,需要的朋友可以參考下
    2014-05-05
  • linux根據(jù)pid獲取進(jìn)程名和獲取進(jìn)程pid(c語言獲取pid)

    linux根據(jù)pid獲取進(jìn)程名和獲取進(jìn)程pid(c語言獲取pid)

    status文件,第一行的Name即為進(jìn)程名,C程序?qū)崿F(xiàn)根據(jù)PID獲取進(jìn)程名和根據(jù)進(jìn)程名獲取PID,大家參考使用吧
    2013-12-12
  • 一篇文章徹底弄懂C++虛函數(shù)的實現(xiàn)機(jī)制

    一篇文章徹底弄懂C++虛函數(shù)的實現(xiàn)機(jī)制

    C++中的虛函數(shù)的作用主要是實現(xiàn)了多態(tài)的機(jī)制,基類定義虛函數(shù),子類可以重寫該函數(shù),在派生類中對基類定義的虛函數(shù)進(jìn)行重寫時,需要在派生類中聲明該方法為虛方法,這篇文章主要給大家介紹了關(guān)于如何通過一篇文章徹底弄懂C++虛函數(shù)的實現(xiàn)機(jī)制,需要的朋友可以參考下
    2021-06-06

最新評論