本题目对应于 C++primer(第四版)中 第十章的文本查询类(TextQuery)

用到的知识有:

顺序容器 vector

关联容器 set,map

标准输入输出流,文件流,字符串流

  1. //写一个文件 从文件中读单词 输出单词的次数和所在的行数
  2. //...
  3. #include <iostream>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <string>
  7. #include <set>
  8. #include <vector>
  9. #include <map>
  10.  
  11. class TextQuery
  12. {
  13. public:
  14. typedef std::vector<std::string>::size_type line_no;
  15. //read the file
  16. void read_file(std::ifstream &is){
  17. store_file(is);
  18. build_map();
  19. }
  20. //query the file return the times every word appears
  21. std::set<line_no> run_query(const std::string&) const;
  22.  
  23. //return the line-number of the word
  24. std::string text_line(line_no) const;
  25.  
  26. private:
  27. void store_file(std::ifstream &is);
  28. void build_map();
  29.  
  30. std::vector<std::string> lines_of_text;
  31. std::map<std::string, std::set<line_no>> word_map;
  32. };
  33.  
  34. void TextQuery::store_file(std::ifstream &is){
  35. std::string textline;
  36. while(std::getline(is, textline)){
  37. lines_of_text.push_back(textline);
  38. }
  39. }
  40.  
  41. void TextQuery::build_map(){
  42. for(line_no line_num = 0; line_num != lines_of_text.size(); ++line_num){
  43. std::istringstream line(lines_of_text[line_num]);
  44. std::string word;
  45. while(line >> word){
  46. word_map[word].insert(line_num);
  47. }
  48. }
  49. }
  50.  
  51. //read to get times of every word appears
  52. std::set<TextQuery::line_no> TextQuery::run_query(const std::string& query_word) const{
  53. std::map<std::string, std::set<line_no>>::const_iterator loc = word_map.find(query_word);
  54.  
  55. if(loc == word_map.end()){
  56. return std::set<line_no>();
  57. }
  58. else{
  59. return loc->second;
  60. }
  61. }
  62.  
  63. //read to get the line-number of the word
  64. std::string TextQuery::text_line(line_no line) const{
  65. if(line < lines_of_text.size()){
  66. return lines_of_text[line];
  67. throw std::out_of_range("line number out of range");
  68. }
  69.  
  70. };
  71.  
  72. std::string make_plural(size_t ctr, const std::string& word, const std::string& ending){
  73. return (ctr == 1) ? word : word + ending;
  74. }
  75.  
  76. void print_results(const std::set<TextQuery::line_no>& locs, const std::string& sought, const TextQuery& file){
  77. typedef std::set<TextQuery::line_no> line_nums;
  78. line_nums::size_type size = locs.size();
  79.  
  80. std::cout<< "\n" << sought << "occurs " << size << " " << make_plural(size, "time", "s") << std::endl;
  81.  
  82. line_nums::const_iterator it = locs.begin();
  83. for(; it != locs.end(); ++it){
  84. std::cout << "\t(line" << (*it) + 1 << ")" <<file.text_line(*it) << std::endl;
  85. }
  86. }
  87.  
  88. int main(){
  89.  
  90. std::ifstream infile("F:\\input.txt");
  91. TextQuery tq;
  92. tq.read_file(infile);
  93.  
  94. while(true){
  95. std::cout << "enter word to look for, or q to quit: ";
  96. std::string s;
  97. std::cin >> s;
  98.  
  99. if(!std::cin || s == "q") break;
  100. // get the set of line numbers on which this word appears
  101. std::set<TextQuery::line_no> locs = tq.run_query(s);
  102. print_results(locs, s, tq);
  103.  
  104. }
  105. return 0;
  106. }

C++primer中的TextQuery(读取文本)的更多相关文章

  1. MySQL中游标使用以及读取文本数据

    原文:MySQL中游标使用以及读取文本数据 前言 之前一直没有接触数据库的学习,只是本科时候修了一本数据库基本知识的课.当时只对C++感兴趣,天真的认为其它的课都没有用,数据库也是半懂不懂,胡乱就考试 ...

  2. Python读取文本,输出指定中文(字符串)

    因业务需求,需要提取文本中带有检查字样的每一行. 样本如下: 1 投入10kVB.C母分段820闭锁备自投压板 2 退出10kVB.C母分段820备投跳803压板 3 退出10kVB.C母分段820备 ...

  3. C#读取文本播放相应语音【转】

    第一种方案: 利用微软text to speech引擎(TTS),读取文本 (1)添加Microsoft Speech Object Library的项目引用 (2)引入using SpeechLib ...

  4. Python循环文件推荐的方式,可用于读取文本最后一行或删除指定行等

    读取文本最后一行: f = open('test11.txt', 'rb') for i in f: offset = -16 while True: f.seek(offset, 2) data = ...

  5. Python实现随机读取文本N行数据

    工作中需要判断某个文本中的URL是否能正常访问,并且随机获取其中N行能正常访问的URL数据,我的思路是:读取文本每一行数据,用urlopen访问,将返回状态码为200的URL保存到一个列表,获得列表长 ...

  6. 解决FileInputStream读取文本时 最后端会多出字符问题

    使用 read(byte[]) 方法读取文本的时候,要用 String str = new String(byte[],int offset,int len) 来将数组中的元素转换为String字符串 ...

  7. c++ 读取文本问题

    c++文本操作有以下三个方法 ifstream,ofstream,fstream 读取文本常用的方法如下 std::ifstream input; input.open(".log" ...

  8. js读取文本内容,支持csv.txt

    js读取文本内容,支持csv.txt <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  9. python逐行读取文本

    一.使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. 二.需要导入import os 三.下面是逐行读取文件内容的三种方法: ...

随机推荐

  1. 简单的下拉刷新以及优化--SwipeRefreshLayout

    代码工程简要说明:以一个SwipeRefreshLayout包裹ListView,SwipeRefreshLayout接管ListView的下拉事件,若ListView被用户触发下拉动作后,Swipe ...

  2. The difference between Union & Union All in SQL Server/pOSTGRESQL

    Following is test in SQL Server: USE [TestDB] CREATE TABLE [dbo].[UserInfoTest02]( [number] [bigint] ...

  3. vs2010的11个调试技巧和方法

    调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总结了可 ...

  4. intellij 设置-试验过的

    1.已修改的文件星号“*”标记 2.在PROJECT窗口中快速定位,编辑窗口中的文件 在编辑的所选文件按ALT+F1, 然后选择PROJECT VIEW 3.改变编辑文本字体大小 FILE -> ...

  5. 在Windows下忘记MySQL最高用户权限密码的解决方案

    1.打开MySQL配置文件 my.ini中,添加上skip-grant-tables,可以添加到文件的末尾或者是这添加到[mysqld]的下面(直接添加在my.ini文件最后亲测可以,但是在[mysq ...

  6. NodeJS下访问SQL Server

    1.下载node-sqlserver (1)msnodesql (msnodesql-0.2.1-v0.8-x64.msi)下载地址:下载  自行选择与自己系统相符的版本,点击安装. (2)msnod ...

  7. 升级iOS10之后调用摄像头/麦克风等硬件程序崩溃闪退的问题

    在升级到iOS10之后, 开发过程中难免会遇到很多的坑, 下面是一些常见的坑, 我做了一些整理, 希望对大家开发有帮助: &1. 调用视频,摄像头, 麦克风,等硬件程序崩溃闪退的问题: 要注意 ...

  8. iOS 10的23个隐藏新特性-b

    上周iOS 10正式版推送后,24小时的更新率已经超过15%,实在惊人.虽然有着初期变砖.5S6卡顿.移动VoLTE无法使用.美版无信号等BUG,但不可忽视的是,iOS 10还是带来了很多从前没有的功 ...

  9. Understand User's Intent from Speech and Text

    http://research.microsoft.com/en-us/projects/IntentUnderstanding/ Understanding what users like to d ...

  10. Netty4.x中文教程系列(四) 对象传输

    Netty4.x中文教程系列(四)  对象传输 我们在使用netty的过程中肯定会遇到传输对象的情况,Netty4通过ObjectEncoder和ObjectDecoder来支持. 首先我们定义一个U ...