电子词典(用map改写)

该博客介绍了如何利用标准库中的Map数据结构来构建一个简洁的电子词典,强调了这种方法的高效和代码的简洁性。
摘要由CSDN通过智能技术生成

优点就是:直接动用标准库函数,很简练


#include<iostream>
//使用下标访问 map 与使用下标访问数组或 vector 的行为截
//然不同:用下标访问不存在的元素将导致在 map 容器中添加一
//个新元素,它的键即为该下标值。
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<map>
#include<vector>
using namespace std;
int main()
{
	map<string,vector<string> >dic;
	ifstream fin;
	fin.open("dictionary.txt",ios_base::in);
	if(!fin){cerr<<"文件没有打开。。"<<endl;exit(1);}
	string word2;
	string word3;
	string word4;
	while(!fin.eof())
	{
        fin>>word2>>word3>>word4;
        vector<string> w;
        w.push_back(word4);
        w.push_back(word3);
	    dic.insert(make_pair(word2,w));
	}
	string word;
	cout<<"输入单词:";
	while(cin>>word)
	{
        map<string,vector<string> >::iterator it=dic.find(word
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值