tuple类型的单词查询例子

简介: 17.3 重写前面的TextQuery程序,使用tuple代替QueryResult类。 TextQuery.h #ifndef TEXTQUERY_H #define TEXTQUERY_H #include #include #include #include #includ...

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()-1;
        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<0>(qr)<<" occurs "<<get<1>(qr)->size()<<" times "<<endl;
    for(auto num:*get<1>(qr))
        os<<"\t(line "<<num+1<<" ) "
        <<(*get<2>(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 < 2 || !(infile.open(argv[1]), infile)) {
        cerr << "No input file!" << endl;
        return EXIT_FAILURE;
    }
    runQueries(infile);
    return 0;
}

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;
};

 

相关文章
|
11月前
|
SQL 存储 关系型数据库
ADBPG&Greenplum成本优化问题之查询大表的dead tuple占比和空间如何解决
ADBPG&Greenplum成本优化问题之查询大表的dead tuple占比和空间如何解决
71 1
[Halcon&笔记] Tuple类型数组
[Halcon&笔记] Tuple类型数组
212 1
antd组件库封装8-Array和tuple类型2
antd组件库封装8-Array和tuple类型2
135 0
antd组件库封装8-Array和tuple类型2
antd组件库封装7-Array和tuple类型1
antd组件库封装7-Array和tuple类型1
118 0
antd组件库封装7-Array和tuple类型1
|
索引 Python
Python知识点笔记-列表list、元组tuple和dict类型
Python知识点笔记-列表list、元组tuple和dict类型
172 0
|
Swift
S(tuple)类及可选(Optional)类型型
<div> <p><span style="font-size:14px;">元组将多个值组合为单个值。元组内的值可以是任意 类型,各元素不必是相同的类型。元组在作为函数返 回值时尤其有用。</span></p> <p><span style="font-size:14px;"> </span></p> <p><span style="font-size:14px;">1、定义方法1</spa
1152 0
|
Python
Python元组tuple“删除”元素的两种函数代码设计
实际上,Python的tuple元组内的元素是不能被修改的,因此也是无法被删除的,但是,为了移除Python元组tuple内的某些元素,以获得一个新的元组,还是有其办法存在的。比如,我们可以使用for循环添加的方法,来创建一个不包含那些需要被移除的元素的新元组。Python中元组添加元素的内置方法为__add__()方法,实际上,该方法也是
145 4
|
存储 索引 Python
元组(Tuple)在Python编程中的应用与实例
元组(Tuple)在Python编程中的应用与实例
354 2
|
存储 缓存 Python
Python中的列表(List)和元组(Tuple)是两种重要的数据结构
【7月更文挑战第12天】Python中的列表(List)和元组(Tuple)是两种重要的数据结构
122 1