博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 单词转换例子
阅读量:2051 次
发布时间:2019-04-28

本文共 763 字,大约阅读时间需要 2 分钟。

今天在看《C++primer》的时候书上有一道例子,于是就自己实现了一遍。

例子的名称叫做单词转换,使用了map对象,难度并不大。

实现思路:

先把单词都存到一个文件里面,文件名叫dictionary.txt。

然后代码如下:

 #include<iostream>

#include<string>
#include<map>
#include<sstream>
#include<fstream>
using namespace std;
void init(map<string, string> &word_change)
{
 string str;
 string front,last;
 ifstream ifs("dictionary.txt");
 while(ifs>>front>>last)
  word_change.insert(make_pair(front,last));
}
void main()
{
 string str,temp,changed="";
 map<string,string> word_change;
 init(word_change);
 getline(cin,str);
 stringstream ss(str);
 while(!ss.eof())
 {
  ss.clear();
  ss >>temp;
  if(word_change.count(temp))
   temp = word_change[temp];
  changed+=temp+" ";
 }
 cout<<changed<<endl;
 system("pause");
}

我在查找单词的时候用的是count函数,也可以用find函数。

拓展:我觉得用类似的方法可以实现英语词典,不过当数据量很大时,查找算法效率不是很高。

 

转载地址:http://wwklf.baihongyu.com/

你可能感兴趣的文章
搜索架构师 一面面经2019年6月
查看>>
稻草人手记
查看>>
第一次kaggle比赛 回顾篇
查看>>
leetcode 50. Pow(x, n)
查看>>
leetcode 130. Surrounded Regions
查看>>
【托业】【全真题库】TEST2-语法题
查看>>
博客文格式优化
查看>>
【托业】【新托业全真模拟】疑难语法题知识点总结(01~05)
查看>>
【SQL】group by 和order by 的区别。
查看>>
【F12】谷歌浏览器--前台效果可以在不访问服务器的前提下直接改样式看效果是否是预期值。...
查看>>
【Python】详解Python多线程Selenium跨浏览器测试
查看>>
Jmeter之参数化
查看>>
Shell 和Python的区别。
查看>>
Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
查看>>
Loadrunner之https协议录制回放报错如何解决?(九)
查看>>
python中xrange和range的异同
查看>>
列表、元组、集合、字典
查看>>
【Python】easygui小甲鱼
查看>>
【Python】关于Python多线程的一篇文章转载
查看>>
【Pyton】【小甲鱼】文件
查看>>