首页 > 代码库 > tuple类型的单词查询例子

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()-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 queryint 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;};

 

tuple类型的单词查询例子