美文网首页
文本查询程序

文本查询程序

作者: 峡迩 | 来源:发表于2017-07-27 22:36 被阅读0次
// 文本查询程序.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<memory>
#include<new>

using namespace std;

using line_no = vector<string>::size_type;

class QueryResult//储存查询结果并输出!
{
public:
    QueryResult(string s, shared_ptr<set<line_no>> p, shared_ptr<vector<string>> f) :find_word(s), lines(p), file(f) {}
    friend ostream &print(ostream &os, const QueryResult &qr);
private:
    string find_word;
    shared_ptr<set<line_no>> lines;
    shared_ptr<vector<string>> file;
};

class TextQuery//读取文件,并返回查询结果至存储类!
{
public:
    TextQuery() = default;
    TextQuery(istream &in){ read(in); }
    void read_from_file(istream &in) { read(in); }
    QueryResult query(const string &find_word) const;                       //创建queryresult对象!

private:
    void read(istream &in);
    shared_ptr<vector<string>> file=make_shared<vector<string>>();          // 如果创建空的只能指针,必须进行绑定(初始化)才可以使用!为什么不能使用new?
    map<string, shared_ptr<set<line_no>>> wm;                               //共享数据,避免拷贝增加计算量!
};

void TextQuery::read(istream &in)
{
    string text;
    while (getline(in, text))
    {
        file->push_back(text);
        int n = file->size() - 1;
        istringstream line(text);
        string word;
        while (line >> word)
        {
            auto &lines = wm[word];
            if (!lines)//如果对应word键值不存在,则创建对象,同时键值为空。通过智能指针reset操作,重新指向new set<line_no>
                lines.reset(new set<line_no>);
            lines->insert(n);
        }
    }
}

QueryResult TextQuery::query(const string &find_word) const
{
    static shared_ptr<set<line_no>> nodata(new set<line_no>);   //静态智能指针,避免函数调用结束后,局部变量释放了!由于只创建了空指针,在读取字符串时可能存在没有绑定内存空间!因此创建初始化静态指针表示未找到单词!(shared_ptr引用则计数加1,计数没减到0,则不会释放呀?)
    auto loc = wm.find(find_word);/
/返回指向数据的迭代器!
    if (loc == wm.end())
        return QueryResult(find_word, nodata, file);
    else
        return QueryResult(find_word, loc->second, file);
}

void runQueries(ifstream &infile)
{
    TextQuery tq(infile);
    while (true)
    {
        string s;
        cout << "Enter a word to look for,or q to quit:";
        if (!(cin >> s) || s == "q")
            break;
        print(cout, tq.query(s)) << endl;
    }
}

ostream &print(ostream &os, const QueryResult &qr)
{
    os << qr.find_word<<" occurs "<<qr.lines->size()<<" times"<<endl;
    for (auto &num : *(qr.lines))
    {
        os << "\t(line " << num + 1 << ") " << *(qr.file->begin() + num) << endl;
    }
    return os;
}


int main()
{
    ifstream file("C:/Users/winack/Documents/Visual Studio 2017/Projects/文本查询程序/123.txt");
    TextQuery t_1;
    t_1.read_from_file(file);
    QueryResult result_you=t_1.query("you");
    print(cout, result_you) << endl;
    file.close();
    system("pause");
    return 0;
}

//面向对象的核心是,抽象继承多态。
抽象把数据实现隐藏,暴露公共接口
继承和多态,派生类继承基类,降低代码重复性,通过指针引用调用派生类,实现多态!

相关文章

  • 使用标准库:文本查询程序

    C++ Primer 5th 第12章动态内存使用标准库:文本查询程序程序名称:文本查询程序程序功能:允许用户在一...

  • 文本查询程序

    //面向对象的核心是,抽象继承多态。抽象把数据实现隐藏,暴露公共接口继承和多态,派生类继承基类,降低代码重复性,通...

  • TextQuery

    C++ Primer 文本查询程序 运行效果 源码

  • 文本查询程序-类

  • ElasticSearch高级查询

    子条件查询 子条件查询指特定字段查询所指特定值 全文本查询 全文本查询针对文本类型数据 字段级别查询 针对结构化数...

  • 文本查询程序-My-类

  • es高级用法

    常用查询 全文本查询 针对文本数据类型 字段基本查询针对结构化数据,如数字,日志期等 模糊匹配 posthttp:...

  • 网球常用

    request 请求 response 文本 Body 响应文本 modify-query 修改查询 请...

  • i/o控制方式

    I/O控制方式主要有程序查询方式、中断方式、DMA方式和通信方式。 1、程序查询方式 程序查询方式也称为程序轮询...

  • SAP MM 为MB51报表增加查询字段

    SAP MM 为MB51报表增加查询字段 比如想按照'抬头文本'字段来查询, 找到'凭证抬头文本',勾选'选择字段...

网友评论

      本文标题:文本查询程序

      本文链接:https://www.haomeiwen.com/subject/gbbtlxtx.html