麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > C > 正文

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

2020-01-26 16:05:31
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
使用map實(shí)現(xiàn)單詞轉(zhuǎn)換的實(shí)例分析
從map中查找單詞時(shí)必須使用find函數(shù),不能使用下表,因?yàn)樵趍ap中使用下標(biāo)訪問(wèn)不存在的元素將導(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
 }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 在火车上摸两乳爽的大叫 | 成人小视频在线播放 | 欧美日韩大片在线观看 | 日本欧美一区二区三区在线播 | 国产一区二区三区黄 | 欧美不卡三区 | 久久亚洲第一 | 久久久久久久国产视频 | 久久成人午夜视频 | 国产一区二区高清在线 | 日本一道aⅴ不卡免费播放 久久久久久久高清 | 91九色免费视频 | 精品一区二区三区在线观看视频 | 亚洲成人激情在线 | 91精品国产九九九久久久亚洲 | 特级毛片免费视频 | 久久久久久久九九九九 | 无码专区aaaaaa免费视频 | 欧美黄一区 | 久久99精品视频在线观看 | 久草在线视频首页 | 日本在线高清 | 欧洲精品久久久久69精品 | 中文字幕亚洲一区二区三区 | hdhdhd69ⅹxxx黑人 | 史上最强炼体老祖动漫在线观看 | 农村少妇吞精夜夜爽视频 | 激情小说激情图片激情电影 | 老a影视网站在线观看免费 国产精品久久久久久久久久尿 | 国产一区二区在线免费播放 | 欧美一级毛片免费观看视频 | 久久久久久久99 | 欧美一区在线观看视频 | 精品国产乱码一区二区 | 91网在线播放 | 久久影院国产精品 | 日韩欧美电影一区二区三区 | 国产美女做爰免费视 | 中文字幕一二三区芒果 | 国产资源在线播放 | 国产免费观看av |