tuple类型的单词查询例子
17.3 重写前面的TextQuery程序,使用tuple代替QueryResult类。
TextQuery.h
#ifndef TEXTQUERY_H
#define TEXTQUERY_H
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<memory>
#include<map>
#include<set>
#include<new>
#include<tuple>
#include"DebugDelete.h"
using namespace std;
class QueryResult;
class TextQuery
{
public:
using line_no=vector<string>::size_type;
TextQuery(ifstream&);
tuple<string,shared_ptr<set<TextQuery::line_no>>,shared_ptr<vector<string>>> query(const string&) const;
~TextQuery()
{
//DebugDelete()(new vector<string>);
cout<<"destructing...."<<endl;
}
private:
shared_ptr<vector<string>> file;
map<string,shared_ptr<set<line_no>>> wm;
};
#endif // TEXTQUERY_H
TextQuery.cpp
#include"TextQuery.h"
#include<tuple>
#include<sstream>
TextQuery::TextQuery(ifstream& is):file(new vector<string>,DebugDelete())
{
string text;
while(getline(is,text))
{
file->push_back(text);
int n=file->size()-;
string word;
istringstream line(text);
while(line>>word)
{
auto &lines=wm[word];
if(!lines)
lines.reset(new set<line_no>);
lines->insert(n);
}
}
} tuple<string,shared_ptr<set<TextQuery::line_no>>,shared_ptr<vector<string>>>
TextQuery::query(const string& sought) const
{
static shared_ptr<set<line_no>> nodata(new set<line_no>);
auto loc=wm.find(sought);
if(loc!=wm.end())
return make_tuple(sought,loc->second,file);
else
return make_tuple(sought,nodata,file);
}
main.cpp
/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:
*
* "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
*
*
* "The authors and publisher have taken care in the preparation of this book,
* but make no expressed or implied warranty of any kind and assume no
* responsibility for errors or omissions. No liability is assumed for
* incidental or consequential damages in connection with or arising out of the
* use of the information or programs contained herein."
*
* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. Send your request for permission, stating clearly
* what code you would like to use, and in what specific way, to the following
* address:
*
* Pearson Education, Inc.
* Rights and Permissions Department
* One Lake Street
* Upper Saddle River, NJ 07458
* Fax: (201) 236-3290
*/ #include <string>
using std::string; #include <fstream>
using std::ifstream; #include <iostream>
using std::cin; using std::cout; using std::cerr;
using std::endl; #include <cstdlib> // for EXIT_FAILURE #include "TextQuery.h"
#include<tuple>
ostream &print(ostream &os,const tuple<string,shared_ptr<set<TextQuery::line_no>>,shared_ptr<vector<string>>> &qr)
{
os<<get<>(qr)<<" occurs "<<get<>(qr)->size()<<" times "<<endl;
for(auto num:*get<>(qr))
os<<"\t(line "<<num+<<" ) "
<<(*get<>(qr))[num]<<endl;
return os;
}
void runQueries(ifstream &infile)
{
// infile is an ifstream that is the file we want to query
TextQuery tq(infile); // store the file and build the query map
// iterate with the user: prompt for a word to find and print results
while (true) {
cout << "enter word to look for, or q to quit: ";
string s;
// stop if we hit end-of-file on the input or if a 'q' is entered
if (!(cin >> s) || s == "q") break;
// run the query and print the results
print(cout, tq.query(s)) << endl;
}
} // program takes single argument specifying the file to query
int main(int argc, char **argv)
{
// open the file from which user will query words
ifstream infile;
// open returns void, so we use the comma operator XREF(commaOp)
// to check the state of infile after the open
if (argc < || !(infile.open(argv[]), infile)) {
cerr << "No input file!" << endl;
return EXIT_FAILURE;
}
runQueries(infile);
return ;
}
DebugDelete.h
#include<iostream>
#include<new>
using namespace std; class DebugDelete
{
public:
DebugDelete(ostream &s=cerr):os(s) {}
template <typename T>
void operator()(T *p) const
{
os<<"deleting shared_ptr "<<endl;
delete p;
}
private:
ostream &os;
};
tuple类型的单词查询例子的更多相关文章
- mysql多表查询例子
[理解方式]先分别找出每个表中查询出来的结果,然后再将两个结果合并. create database test charset utf8 collate utf8_bin;use test;creat ...
- Tuple类型
Tuple类型类似的体现了C#中的匿名类型 var person=new { Name="Eric"; Age=18: } 调用: Console.writeline( perso ...
- [System.Net]模拟Web请求编写简易单词查询客户端
demo: 我就不上传了 前言 在实际生活中,网络请求的应用极其常见,比如使用浏览器,程序中我们还要调用webservice.那么浏览器是怎么请求网络资源的呢?不用它可以自己请求不? 答案是可以的. ...
- python学习第五天 List和tuple类型介绍及其List切片
List 和tuple: python提供一种类似C语言数组的类型,但是使用起来确是相当的简洁.那就讲讲这神奇的python中list 和tuple吧. List类型: 1.直接贴代码: L = [' ...
- Scala Tuple类型
Tuple可以作为集合存储不同类型的数据,初始化实例如下: val tuple = (1,3,3.14,"aa") val third = tuple._3 Tuple 下标访问从 ...
- Python实现单词查询&文件查找
最近学C++ Primer,做到第十二章有个习题.要求针对英文文本,对于用户想搜索的单词,打印出该单词在文本中出现的总次数,单词所出现行号及对应的行内容:单词在一行内出现多次,只打印该行一次.C++的 ...
- .net 4.0 中的特性总结(四):Tuple类型
Tuple是具有指定数量和顺序的值的一种数据结构.针对这种数据结构,.Net4.0中提供了一组Tuple类型,具体如下: Tuple Tuple<T> Tuple<T1, T ...
- 编写高质量代码改善C#程序的157个建议——建议26:使用匿名类型存储LINQ查询结果
建议26:使用匿名类型存储LINQ查询结果 从.NET3.0开始,C#开始支持一个新特性:匿名类型.匿名类型有var.赋值运算符和一个非空初始值(或以new开头的初始化项)组成.匿名类型有如下基本特性 ...
- Python之List和Tuple类型(入门3)
转载请标明出处: http://www.cnblogs.com/why168888/p/6407682.html 本文出自:[Edwin博客园] Python之List和Tuple类型 1. Pyth ...
随机推荐
- Java多线程初学者指南(9):为什么要进行数据同步
Java中的变量分为两类:局部变量和类变量.局部变量是指在方法内定义的变量,如在run方法中定义的变量.对于这些变量来说,并不存在线程之间共享的问题.因此,它们不需要进行数据同步.类变量是在类中定义的 ...
- 高远介绍的好东东--django-celery
终于可以到异步消息机制的高档产品啦~~~ 不知能不能代替AJAX.. 参照官方文档试下: 中文文档: http://docs.jinkan.org/docs/celery/getting-starte ...
- [转贴]xcode帮助文档
突然间得到了一台MAC ,这时候不学OC 更待何时学呀?马上找了IOS开发的书和网上的帖子看,最近在开源力量那里看了TINYFOOL的入门讲座,讲的都很虚,可能时间不够吧,也没看到什么例子呀,什么的, ...
- 【Spring】Spring IOC原理及源码解析之scope=request、session
一.容器 1. 容器 抛出一个议点:BeanFactory是IOC容器,而ApplicationContex则是Spring容器. 什么是容器?Collection和Container这两个单词都有存 ...
- Android用户界面 UI组件--AdapterView及其子类(一) ListView及各种Adapter详解
ListView就是列表组件,一般通过继承ListActivity使用系统提供的ListView. 所有的AdapterView组件都需要有一个对应的Adapter作为适配器来显示列表中元素的布局方式 ...
- AD域环境的搭建 基于Server 2008 R2
AD(Active Directory)即活动目录,微软的基础件.微软的很多产品如:Exchange Server,Lync Server,SharePoint Server,Forefront Se ...
- maven 搭建企业级web项目
就看这篇文章了:http://www.cnblogs.com/quanyongan/archive/2013/05/28/3103243.html
- linux上怎么切换不同版本的arm-linux-gcc?只需改一行函数
linux上怎么切换不同版本的arm-linux-gcc?只需改一行函数 ln -s /usr/local/arm/3.4.1/bin/arm-linux-gcc /usr/bin/arm-linux ...
- bzoj2004
反正N<=10^9肯定是矩阵乘法反正p<=10肯定是状压dp首先有一个非常重要的性质是任意连续P个站,必须保证K辆车必须停在其中的一个站我们设f[i,S]表示到第i个站搞定了后,这K辆公交 ...
- HDU 5965 扫雷 【模拟】 (2016年中国大学生程序设计竞赛(合肥))
扫雷 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...