最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2013年05月28日 15:38:28   作者:  
本篇文章是對(duì)使用map實(shí)現(xiàn)單詞轉(zhuǎn)換的代碼實(shí)例進(jìn)行了纖細(xì)的分析介紹,需要的朋友參考下
使用map實(shí)現(xiàn)單詞轉(zhuǎn)換的實(shí)例分析
從map中查找單詞時(shí)必須使用find函數(shù),不能使用下表,因?yàn)樵趍ap中使用下標(biāo)訪問不存在的元素將導(dǎo)致在map容器中添加一個(gè)新的元素,新元素的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++中string與int的相互轉(zhuǎn)換實(shí)現(xiàn)代碼

    C++中string與int的相互轉(zhuǎn)換實(shí)現(xiàn)代碼

    這篇文章主要介紹了C++中string與int的相互轉(zhuǎn)換實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2017-05-05
  • C/C++?控制臺(tái)等待指令解析

    C/C++?控制臺(tái)等待指令解析

    這篇文章主要介紹了C/C++?控制臺(tái)等待指令解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • C++中putchar與getchar函數(shù)的細(xì)節(jié)及運(yùn)用

    C++中putchar與getchar函數(shù)的細(xì)節(jié)及運(yùn)用

    C語(yǔ)言提供putchar函數(shù),用于給終端輸出一個(gè)字符;getchar函數(shù),可以從終端接收用戶輸入的一個(gè)字符,本文給大家分享C++中putchar與getchar函數(shù)的細(xì)節(jié)及運(yùn)用,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • C++?使用?new?創(chuàng)建二維數(shù)組實(shí)例

    C++?使用?new?創(chuàng)建二維數(shù)組實(shí)例

    這篇文章主要介紹了C++?使用?new?創(chuàng)建二維數(shù)組實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • 淺析C++函數(shù)模板和類模板

    淺析C++函數(shù)模板和類模板

    C++語(yǔ)言的模板技術(shù)包括函數(shù)模板和類模板,模板技術(shù)是一種代碼重用技術(shù),函數(shù)和類是C++語(yǔ)言中兩種主要的重用代碼形式,這篇文章主要介紹了C++函數(shù)模板和類模板,需要的朋友可以參考下
    2022-07-07
  • vscode遠(yuǎn)程連接服務(wù)器(免密登錄+遠(yuǎn)程開發(fā))

    vscode遠(yuǎn)程連接服務(wù)器(免密登錄+遠(yuǎn)程開發(fā))

    vscode的遠(yuǎn)程連接功能十分方便,本文就來(lái)介紹一下vscode遠(yuǎn)程連接服務(wù)器,主要包括免密登錄和遠(yuǎn)程開發(fā),感興趣的可以了解一下
    2024-07-07
  • C++中new/delete與malloc/free的區(qū)別小結(jié)

    C++中new/delete與malloc/free的區(qū)別小結(jié)

    本文主要介紹了C++中new/delete與malloc/free的區(qū)別小結(jié), malloc、free是C中的庫(kù)函數(shù) new、delete 是C++當(dāng)中的操作符,讀者可以更好地理解C++中內(nèi)存管理的方式和優(yōu)勢(shì)
    2023-08-08
  • C++和C的混合編譯的項(xiàng)目實(shí)踐

    C++和C的混合編譯的項(xiàng)目實(shí)踐

    本文主要介紹了C++和C的混合編譯的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 基于C語(yǔ)言實(shí)現(xiàn)的迷宮游戲代碼

    基于C語(yǔ)言實(shí)現(xiàn)的迷宮游戲代碼

    這篇文章主要介紹了基于C語(yǔ)言實(shí)現(xiàn)的迷宮游戲代碼,對(duì)于學(xué)習(xí)游戲開發(fā)的朋友相信有一定的借鑒價(jià)值,需要的朋友可以參考下
    2014-08-08
  • C BlowFish對(duì)稱加密算法詳解

    C BlowFish對(duì)稱加密算法詳解

    這篇文章主要介紹了C BlowFish對(duì)稱加密算法詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評(píng)論

西盟| 尚义县| 红桥区| 全州县| 调兵山市| 梨树县| 彩票| 星子县| 辛集市| 清水河县| 揭东县| 乌拉特中旗| 连城县| 道孚县| 南岸区| 固阳县| 海阳市| 湘西| 吉林省| 康定县| 桑植县| 耿马| 嘉荫县| 雅安市| 仙桃市| 宜阳县| 和田县| 韶山市| 广汉市| 庄浪县| 巴塘县| 通山县| 会昌县| 望江县| 司法| 香港| 碌曲县| 宜州市| 滦南县| 荆州市| 嘉鱼县|