




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
本篇实例的目的是做一个简单的文本查询程序,通过编码过程有益于您对vector, map, set的基本掌握。/*目的:一个简单的文本查询程序*作用:程序将读取用户指定的任意文本文件,然后允许用户从该文件中查找单词。*查询的结果是该单词出现的次数,并列出每次出现所在的行。*如果某单词在同一行中多次出现,程序将只显示该行一次。*行号按升序显示,即第 7 行应该在第 9 行之前输出,依此类推。*/*思路:*1.使用一个 vector 类型的对象存储整个输入文件的副本。* 输入文件的每一行是该 vector 对象的一个元素。* 因而,在希望输出某一行时,只需以行号为下标获取该行所在的元素即可。*2.将每个单词所在的行号存储在一个 set 容器对象中。* 使用 set 就可确保每行只有一个条目,而且行号将自动按升序排列。*3.使用一个 map 容器将每个单词与一个 set 容器对象关联起来,* 该 set 容器对象记录此单词所在的行号。*/TextQuery.H文件#ifndef TEXTQUERY_H#define TEXTQUERY_H#include #include #include #include #include #include #include #include class TextQuery / as beforepublic: / typedef to make declarations easier typedef std:string:size_type str_size; typedef std:vector:size_type line_no; /* interface: * read_file builds internal data structures for the given file * run_query finds the givenwordand returns set of lines on which it appears * text_line returns a requested line from the input file */ void read_file(std:ifstream &is) store_file(is); build_map(); std:set run_query(const std:string&) const; std:string text_line(line_no) const; str_size size() const return lines_of_text.size(); void display_map(); / debugging aid: print the mapprivate: / utility functions used by read_file void store_file(std:ifstream&); / store input file void build_map(); / associated each word with a set of line numbers / remember the whole input file std:vector lines_of_text; / map word to set of the lines on which it occurs std:map std:string, std:set word_map; / characters that constitute whitespace static std:string whitespace_chars; / canonicalizes text: removes punctuation and makes everything lower case static std:string cleanup_str(const std:string&);#endifTextQuery.CPP 文件#include TextQuery.h#include #include #include #include #include #include #include #include #include #include using std:istringstream;using std:set;using std:string;using std:getline;using std:map;using std:vector;using std:cerr;using std:cout;using std:cin;using std:ifstream;using std:endl;using std:ispunct;using std:tolower;using std:strlen;using std:out_of_range;string TextQuery:text_line(line_no line) const if (line word) / add this line number to the set; / subscript will add word to the map if its not already there word_mapcleanup_str(word).insert(line_num); setTextQuery:run_query(const string &query_word) const / Note: must use find and not subscript the map directly / to avoid adding words to word_map! mapstring, set :const_iterator loc = word_map.find(cleanup_str(query_word); if (loc = word_map.end() return set(); / not found, return empty set else / fetch and return set of line numbers for this word return loc-second;void TextQuery:display_map() map string, set :iterator iter = word_map.begin(), iter_end = word_map.end(); / for each word in the map for ( ; iter != iter_end; +iter) cout word: first ; / fetch location vector as a const reference to avoid copying it const set &text_locs = iter-second; set:const_iterator loc_iter = text_locs.begin(), loc_iter_end = text_locs.end(); / print all line numbers for this word while (loc_iter != loc_iter_end) cout *loc_iter; if (+loc_iter != loc_iter_end) cout , ; cout n; / end list of output this word cout endl; / finished printing entire map/ lower-case to upper-casestring TextQuery:cleanup_str(const string &word) string ret; for (string:const_iterator it = word.begin(); it != word.end(); +it) if (!ispunct(*it) ret += tolower(*it); return ret;主函数#include TextQuery.h#include #include #include #include #include #include #include #include #include using std:set;using std:string;using std:map;using std:vector;using std:cerr;using std:cout;using std:cin;using std:ifstream;using std:endl;string make_plural(size_t, const string&, const string&);ifstream& open_file(ifstream&, const string&);void print_results(const set& locs, const string& sought, const TextQuery &file) / if the word was found, then print count and all occurrences typedef set line_nums; line_nums:size_type size = locs.size(); cout n sought occurs size make_plural(size, time, s) endl; / print each line in which the word appeared line_nums:const_iterator it = locs.begin(); for ( ; it != locs.end(); +it) cout t(line / dont confound user with text lines starting at 0 (*it) + 1 ) file.text_line(*it) endl; / program takes single argument specifying the file to queryint main() / open the file from which user will query words ifstream infile; if (!open_file(infile, Tanky_Woo.txt) cerr No input file! endl; return EXIT_FAILURE; TextQuery tq; tq.read_file(infile); / builds query map / iterate with the user: prompt for a word to find and print results / loop indefinitely; the loop exit is inside the while while (true) cout s; / stop if hit eof on input or a q is entered if (!cin | s = q) break; / get the set of line numbers on which this word appears set locs = tq.run_query(s); / print count and all occurrences, if any print_results(locs, s, tq); return 0;string make_plural (size_t ctr , const string &word ,const string &ending) return ( ctr = 1 ) ? word : word + ending;ifstream& open_file(ifstream &in, const st
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年绿色建筑设计与绿色建材应用研究报告
- 网王离婚协议书
- 2025年新能源行业国际合作与竞争策略报告
- 2025安徽阜阳市颍州区选调区内乡镇在编在岗教师60人考前自测高频考点模拟试题有答案详解
- 2025年新能源汽车租赁市场创新与商业模式研究报告
- 往来抵消协议书
- 微博协议书群发
- 多租房协议书
- 2025福建省海峡人才报社有限责任公司招聘见习生模拟试卷完整答案详解
- 金融衍生品市场创新业务模式2025年风险防范与合规分析报告
- 2025即时零售行业规模消费场景及头部平台美团京东淘宝对比分析报告
- 高密财政债务管理办法
- 观光园艺课件教学
- 英语的历史课件
- 华兴数控WA-32XTA用户手册
- 生成式AI在动画短片场景设计中的创新应用
- 《铁路路基施工与维护》高职高速铁路施工与维护全套教学课件
- 2025-2030年中国手持三维激光扫描仪行业市场深度分析及发展趋势与投资前景研究报告
- 2025-2030中国中药材银杏叶行业供需格局及未来销售渠道趋势报告
- 潍坊市公安局招录警务辅助人员笔试真题2024
- 精准农业中微生物群落的调控机制-洞察阐释
评论
0/150
提交评论