C++ 单词查询程序

这是一个使用C++编写的单词查询程序,程序通过ifstream读取文件,利用vector存储文本行,map和set来查找和存储单词及其所在行号。用户可以输入单词进行查询,程序会打印出该单词出现的次数及对应行号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 这是我自己写的一个单词查询程序,虽然还可以将代码写简洁一些,但我觉得用多了typedef,反而影响了代码的可读性,所以这个程序的代码看起来较冗杂。

#include<fstream>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<sstream>
using namespace std;
// 打开文件
ifstream& open_file(ifstream& infile,const string file)
{
 infile.close();
 infile.clear();
 infile.open(file.c_str());
 return infile;
}
// 类定义
class solution
{
public:
 typedef vector<string> lines;
 // 获取单词和文本
 void read_file(ifstream& infile)
 {
  read_line(infile);
  read_word();
 }
 // 查找单词
 set<lines::size_type> find_word(string& word)
 {
  map<string,set<lines::size_type>>::iterator loc = get_word.find(word);
  if(loc != get_word.end())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值