17.3 重写前面的TextQuery程序,使用tuple代替QueryResult类。

TextQuery.h

  1. #ifndef TEXTQUERY_H
  2. #define TEXTQUERY_H
  3. #include<iostream>
  4. #include<string>
  5. #include<fstream>
  6. #include<vector>
  7. #include<memory>
  8. #include<map>
  9. #include<set>
  10. #include<new>
  11. #include<tuple>
  12. #include"DebugDelete.h"
  13. using namespace std;
  14. class QueryResult;
  15. class TextQuery
  16. {
  17. public:
  18. using line_no=vector<string>::size_type;
  19. TextQuery(ifstream&);
  20. tuple<string,shared_ptr<set<TextQuery::line_no>>,shared_ptr<vector<string>>> query(const string&) const;
  21. ~TextQuery()
  22. {
  23. //DebugDelete()(new vector<string>);
  24. cout<<"destructing...."<<endl;
  25. }
  26. private:
  27. shared_ptr<vector<string>> file;
  28. map<string,shared_ptr<set<line_no>>> wm;
  29. };
  30. #endif // TEXTQUERY_H

TextQuery.cpp

  1. #include"TextQuery.h"
  2. #include<tuple>
  3. #include<sstream>
  4. TextQuery::TextQuery(ifstream& is):file(new vector<string>,DebugDelete())
  5. {
  6. string text;
  7. while(getline(is,text))
  8. {
  9. file->push_back(text);
  10. int n=file->size()-;
  11. string word;
  12. istringstream line(text);
  13. while(line>>word)
  14. {
  15. auto &lines=wm[word];
  16. if(!lines)
  17. lines.reset(new set<line_no>);
  18. lines->insert(n);
  19. }
  20. }
  21. }
  22.  
  23. tuple<string,shared_ptr<set<TextQuery::line_no>>,shared_ptr<vector<string>>>
  24. TextQuery::query(const string& sought) const
  25. {
  26. static shared_ptr<set<line_no>> nodata(new set<line_no>);
  27. auto loc=wm.find(sought);
  28. if(loc!=wm.end())
  29. return make_tuple(sought,loc->second,file);
  30. else
  31. return make_tuple(sought,nodata,file);
  32. }

main.cpp

  1. /*
  2. * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
  3. * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
  4. * copyright and warranty notices given in that book:
  5. *
  6. * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
  7. *
  8. *
  9. * "The authors and publisher have taken care in the preparation of this book,
  10. * but make no expressed or implied warranty of any kind and assume no
  11. * responsibility for errors or omissions. No liability is assumed for
  12. * incidental or consequential damages in connection with or arising out of the
  13. * use of the information or programs contained herein."
  14. *
  15. * Permission is granted for this code to be used for educational purposes in
  16. * association with the book, given proper citation if and when posted or
  17. * reproduced.Any commercial use of this code requires the explicit written
  18. * permission of the publisher, Addison-Wesley Professional, a division of
  19. * Pearson Education, Inc. Send your request for permission, stating clearly
  20. * what code you would like to use, and in what specific way, to the following
  21. * address:
  22. *
  23. * Pearson Education, Inc.
  24. * Rights and Permissions Department
  25. * One Lake Street
  26. * Upper Saddle River, NJ 07458
  27. * Fax: (201) 236-3290
  28. */
  29.  
  30. #include <string>
  31. using std::string;
  32.  
  33. #include <fstream>
  34. using std::ifstream;
  35.  
  36. #include <iostream>
  37. using std::cin; using std::cout; using std::cerr;
  38. using std::endl;
  39.  
  40. #include <cstdlib> // for EXIT_FAILURE
  41.  
  42. #include "TextQuery.h"
  43. #include<tuple>
  44. ostream &print(ostream &os,const tuple<string,shared_ptr<set<TextQuery::line_no>>,shared_ptr<vector<string>>> &qr)
  45. {
  46. os<<get<>(qr)<<" occurs "<<get<>(qr)->size()<<" times "<<endl;
  47. for(auto num:*get<>(qr))
  48. os<<"\t(line "<<num+<<" ) "
  49. <<(*get<>(qr))[num]<<endl;
  50. return os;
  51. }
  52. void runQueries(ifstream &infile)
  53. {
  54. // infile is an ifstream that is the file we want to query
  55. TextQuery tq(infile); // store the file and build the query map
  56. // iterate with the user: prompt for a word to find and print results
  57. while (true) {
  58. cout << "enter word to look for, or q to quit: ";
  59. string s;
  60. // stop if we hit end-of-file on the input or if a 'q' is entered
  61. if (!(cin >> s) || s == "q") break;
  62. // run the query and print the results
  63. print(cout, tq.query(s)) << endl;
  64. }
  65. }
  66.  
  67. // program takes single argument specifying the file to query
  68. int main(int argc, char **argv)
  69. {
  70. // open the file from which user will query words
  71. ifstream infile;
  72. // open returns void, so we use the comma operator XREF(commaOp)
  73. // to check the state of infile after the open
  74. if (argc < || !(infile.open(argv[]), infile)) {
  75. cerr << "No input file!" << endl;
  76. return EXIT_FAILURE;
  77. }
  78. runQueries(infile);
  79. return ;
  80. }

DebugDelete.h

  1. #include<iostream>
  2. #include<new>
  3. using namespace std;
  4.  
  5. class DebugDelete
  6. {
  7. public:
  8. DebugDelete(ostream &s=cerr):os(s) {}
  9. template <typename T>
  10. void operator()(T *p) const
  11. {
  12. os<<"deleting shared_ptr "<<endl;
  13. delete p;
  14. }
  15. private:
  16. ostream &os;
  17. };

tuple类型的单词查询例子的更多相关文章

  1. mysql多表查询例子

    [理解方式]先分别找出每个表中查询出来的结果,然后再将两个结果合并. create database test charset utf8 collate utf8_bin;use test;creat ...

  2. Tuple类型

    Tuple类型类似的体现了C#中的匿名类型 var person=new { Name="Eric"; Age=18: } 调用: Console.writeline( perso ...

  3. [System.Net]模拟Web请求编写简易单词查询客户端

    demo: 我就不上传了 前言 在实际生活中,网络请求的应用极其常见,比如使用浏览器,程序中我们还要调用webservice.那么浏览器是怎么请求网络资源的呢?不用它可以自己请求不? 答案是可以的. ...

  4. python学习第五天 List和tuple类型介绍及其List切片

    List 和tuple: python提供一种类似C语言数组的类型,但是使用起来确是相当的简洁.那就讲讲这神奇的python中list 和tuple吧. List类型: 1.直接贴代码: L = [' ...

  5. Scala Tuple类型

    Tuple可以作为集合存储不同类型的数据,初始化实例如下: val tuple = (1,3,3.14,"aa") val third = tuple._3 Tuple 下标访问从 ...

  6. Python实现单词查询&文件查找

    最近学C++ Primer,做到第十二章有个习题.要求针对英文文本,对于用户想搜索的单词,打印出该单词在文本中出现的总次数,单词所出现行号及对应的行内容:单词在一行内出现多次,只打印该行一次.C++的 ...

  7. .net 4.0 中的特性总结(四):Tuple类型

    Tuple是具有指定数量和顺序的值的一种数据结构.针对这种数据结构,.Net4.0中提供了一组Tuple类型,具体如下: Tuple   Tuple<T>   Tuple<T1, T ...

  8. 编写高质量代码改善C#程序的157个建议——建议26:使用匿名类型存储LINQ查询结果

    建议26:使用匿名类型存储LINQ查询结果 从.NET3.0开始,C#开始支持一个新特性:匿名类型.匿名类型有var.赋值运算符和一个非空初始值(或以new开头的初始化项)组成.匿名类型有如下基本特性 ...

  9. Python之List和Tuple类型(入门3)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6407682.html 本文出自:[Edwin博客园] Python之List和Tuple类型 1. Pyth ...

随机推荐

  1. 根据WSDL生成代理类方式(2)

    运行开发人员工具提示 输入命令行svcutil http://localhost:8080/Test/TestClassPort?wsdl

  2. QEvent整理归纳:140种类型,29个继承类,7个函数,3种事件来源

    140种事件类型: QEvent::None QEvent::AccessibilityDescription QEvent::AccessibilityHelp QEvent::Accessibil ...

  3. 12.URL重写

    为什么要URL重写?1.有利于SEO(搜索引擎优化),带参数的RUL权重较低.2.地址看起来更正规,推广uid. 如我们一般在访问网页是会带参数,http://aaa.com/view.htm?id= ...

  4. andriod系统裁剪心得

    亲们,,有人做过 将android系统总内存减少,并保持系统的稳定运行 方面的么?...比如将512M的内存换成256M,系统依然稳定运行,, 我目前,从三个方面下手,,,1.删减系统中不需要使用的a ...

  5. 一起啃PRML - 1.2.2 Expectations and covariances 期望和协方差

    一起啃PRML - 1.2.2 Expectations and covariances 期望和协方差 @copyright 转载请注明出处 http://www.cnblogs.com/chxer/ ...

  6. PySpark关于HDFS文件(目录)输入、数据格式的探讨

    背景   平台HDFS数据存储规则是按照“数据集/天目录/小时目录/若干文件”进行的,其中数据集是依据产品线或业务划分的.   用户分析数据时,可能需要处理以下五个场景:   (一)分析指定数据集.指 ...

  7. 昂贵的聘礼--POJ1062

    昂贵的聘礼 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submiss ...

  8. 论在Repository中使用EF框架

    最近在思考框架的事情,从Petshop的传统三层框架过渡到目前的DDD模式. 目前纠结的几个节点是: 1,EF这个ORM框架,有没有必要在 Repository 层封装一下,或者直接在 Service ...

  9. 整理:Google jQuery 引用地址大全和方法(转)

    什么是google的js托管? 说的明白点,跟我们以往做法一样,只不过这时候的引用的js库是放在google服务器上的. 比如引用jquery,则使用路径  http://ajax.googleapi ...

  10. 关于 RecastNavigation 寻路结果异常的问题。

    由于我们的项目采用的寻路解决方案是:客户端使用 unity 原生的寻路系统,服务器采用 RecastNavigation 系统,而服务器的寻路数据来自于从 unity 导出的,所以理论上两边的寻路结果 ...