使用map实现单词转换的实例分析

 更新时间:2013年05月28日 15:38:28   作者:  
本篇文章是对使用map实现单词转换的代码实例进行了纤细的分析介绍,需要的朋友参考下
使用map实现单词转换的实例分析
从map中查找单词时必须使用find函数,不能使用下表,因为在map中使用下标访问不存在的元素将导致在map容器中添加一个新的元素,新元素的key即要查找的内容。
复制代码 代码如下:

/*****************************************************************************
* 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
 }
}

相关文章

  • C++中string与int的相互转换实现代码

    C++中string与int的相互转换实现代码

    这篇文章主要介绍了C++中string与int的相互转换实现代码,需要的朋友可以参考下
    2017-05-05
  • C/C++ 控制台等待指令解析

    C/C++ 控制台等待指令解析

    这篇文章主要介绍了C/C++ 控制台等待指令解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • C++中putchar与getchar函数的细节及运用

    C++中putchar与getchar函数的细节及运用

    C语言提供putchar函数,用于给终端输出一个字符;getchar函数,可以从终端接收用户输入的一个字符,本文给大家分享C++中putchar与getchar函数的细节及运用,感兴趣的朋友跟随小编一起看看吧
    2021-07-07
  • C++ 使用 new 创建二维数组实例

    C++ 使用 new 创建二维数组实例

    这篇文章主要介绍了C++ 使用 new 创建二维数组实例的相关资料,需要的朋友可以参考下
    2023-01-01
  • 浅析C++函数模板和类模板

    浅析C++函数模板和类模板

    C++语言的模板技术包括函数模板和类模板,模板技术是一种代码重用技术,函数和类是C++语言中两种主要的重用代码形式,这篇文章主要介绍了C++函数模板和类模板,需要的朋友可以参考下
    2022-07-07
  • vscode远程连接服务器(免密登录+远程开发)

    vscode远程连接服务器(免密登录+远程开发)

    vscode的远程连接功能十分方便,本文就来介绍一下vscode远程连接服务器,主要包括免密登录和远程开发,感兴趣的可以了解一下
    2024-07-07
  • C++中new/delete与malloc/free的区别小结

    C++中new/delete与malloc/free的区别小结

    本文主要介绍了C++中new/delete与malloc/free的区别小结, malloc、free是C中的库函数 new、delete 是C++当中的操作符,读者可以更好地理解C++中内存管理的方式和优势
    2023-08-08
  • C++和C的混合编译的项目实践

    C++和C的混合编译的项目实践

    本文主要介绍了C++和C的混合编译的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • 基于C语言实现的迷宫游戏代码

    基于C语言实现的迷宫游戏代码

    这篇文章主要介绍了基于C语言实现的迷宫游戏代码,对于学习游戏开发的朋友相信有一定的借鉴价值,需要的朋友可以参考下
    2014-08-08
  • C BlowFish对称加密算法详解

    C BlowFish对称加密算法详解

    这篇文章主要介绍了C BlowFish对称加密算法详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08

最新评论