A simple Test-Query Program

/*************************************************************************
 *
 *  Using the library: A Simple Test-Query Program
 *
 ************************************************************************/

#include<iostream>
#include<vector>
#include<string>
#include<memory>  //shared_ptr
#include<map>
#include<fstream>
#include<set>
#include<sstream> //istringstream

using namespace std;
typedef vector<string>::size_type line_no;

class QueryResult
{
friend ostream& print(ostream&,const QueryResult&);
public:
    QueryResult(string s,shared_ptr<set<line_no>> p,shared_ptr<vector<string>> f):
        sought(s),lines(p),file(f){}
private:
    string sought;
    shared_ptr<set<line_no>> lines;
    shared_ptr<vector<string>> file;
};

class TextQuery
{
public:
    TextQuery(ifstream&);
    QueryResult query(const string&) const;
private:
    shared_ptr<vector<string>> file;
    map<string,shared_ptr<set<line_no>>> wm;
};

TextQuery::TextQuery(ifstream &is):file(new vector<string>)
{
    string lineText;
    while(getline(is,lineText))
    {
        file->push_back(lineText);
        line_no n=file->size();   //current line number;
        istringstream line(lineText);
        string word;
        while(line>>word)
        {
            auto &lines=wm[word];  //lines is a share_ptr<set<line_no>>
            if(!lines)
                lines.reset(new set<line_no>);
            lines->insert(n);
        }
    }
}
QueryResult 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 QueryResult(sought,nodata,file);
    else
        return QueryResult(sought,loc->second,file);
}

ostream &print(ostream &os,const QueryResult &qr)
{
    os << qr.sought << " occurs " << qr.lines->size();

    if(qr.lines->size()>1)
        os<<" times"<<endl;
    else
        os<<" time"<<endl;

    for(auto num : *qr.lines)
        os << "(line"<<num<<")    "<< *(qr.file->begin()+num-1)<<endl;
    return os;
}

int main()
{
    ifstream  infile("1.txt");  //input a file,filename
    TextQuery tq(infile);
    while(true)
    {
        cout<< "enter the word to look for,or q to quit: "<<endl;
        string s;
        if(!(cin >> s) || s=="q") break;   //input the search word
        print(cout,tq.query(s))<<endl;
    }
    return 0;
}
时间: 2024-08-30 13:55:17

A simple Test-Query Program的相关文章

FluentData -Micro ORM with a fluent API that makes it simple to query a database 【MYSQL】

官方地址:http://fluentdata.codeplex.com/documentation MYSQL: MySQL through the MySQL Connector .NET driver. 连接字符串:Server=127.0.0.1;Database=testDB;Uid=root;Pwd=jnex;<system.data> <DbProviderFactories> <add name="MySQL Data Provider" i

[Coding Made Simple] Sum Query in 2D Immutable Array

Given a 2D immutable array,  Write an efficient program to support any given sub-rectangle sum query in this 2D matrix. A simple solution is to add each entry inside the sub-rectangle. The runtime is O(n * m). The problem of this solution is that for

FluentData -Micro ORM with a fluent API that makes it simple to query a database

Code samples Create and initialize a DbContextThe connection string on the DbContext class can be initialized either by giving the connection string name in the *.config file or by sending in the entire connection string. Important configurations Ign

SQL optimizer -Query Optimizer Deep Dive

refer: http://sqlblog.com/blogs/paul_white/archive/2012/04/28/query-optimizer-deep-dive-part-1.aspx    SQL是一种结构化查询语言规范,它从逻辑是哪个描述了用户需要的结果,而SQL服务器将这个逻辑需求描述转成能执行的物理执行计划,从而把结果返回给用户.将逻辑需求转换成一个更有效的物理执行计划的过程,就是优化的过程. 执行SQL的过程: Input Tree We start by looking

Debugging a SQL Server query with WinDbg

Debugging a SQL Server query with WinDbg May 13, 2014 · Klaus Aschenbrenner · 5 Comments (Be sure to checkout the FREE SQLpassion Performance Tuning Training Plan - you get a weekly email packed with all the essential knowledge you need to know about

Query DSL for elasticsearch Query

Query DSL Query DSL (资料来自: http://www.elasticsearch.cn/guide/reference/query-dsl/) http://elasticsearch.qiniudn.com/ --简介-- elasticsearch 提供基于JSON的完整的Query DSL查询表达式(DSL即领域专用语言). 一般来说, 普通的查询如 term 或者 prefix. 另外还有混合查询如 bool 等. 另外查询表达式(Queries)还能够关联特定的过

Java Secure Socket Extension (JSSE) Reference Guide

Skip to Content Oracle Technology Network Software Downloads Documentation Search Java Secure Socket Extension (JSSE) Reference Guide This guide covers the following topics: Skip Navigation Links Introduction Features and Benefits JSSE Standard API S

Rewrite MSIL Code on the Fly with the .NET Framework Profiling API

.NET Internals Rewrite MSIL Code on the Fly with the .NET Framework Profiling API Aleksandr Mikunov This article assumes you're familiar with the CLR and C# Level of Difficulty 1 2 3 Code download available at: NETProfilingAPI.exe (2,901KB) SUMMARY I

C#第六节课

首先介绍了LINQ的用法 LINQ就是一个C#自带的数据库,实现的功能与主流的关系型数据库基本一致 ,它在对象领域和数据领域之间架起了一座桥梁,往往我们在编写日常的应用程序的时候很难绕开数据库的应用 在 Visual Studio 中,可以用 Visual Basic 或 C# 为以下数据源编写 LINQ 查询:SQL Server 数据库.XML 文档.ADO.NET 数据集,以及支持 IEnumerable 或泛型 IEnumerable<T> 接口的任意对象集合. 此外,还计划了对 AD

C#自己的数据库语言LINQ(1)

与其他语言不太相同,C#语言本身有着自己的数据库查询语言,叫做LINQ,全称Language-INtegrated Query. 在很大程度上,LINQ与我们常用的SQL是相通的,但是却有着更加独特的一些特性.由于程序员每天都要对内存.数据库或是XML文件中的数据对象进行查找和存取,但SQL语言同编程语言的分离为这种行为造成了很大的困扰. 而LINQ作为一种连接面向对象语言和关系数据库的桥梁,联合了对内存.数据库和XML的数据处理.同时作为C#的一种内部语言,写法上更有利于C#程序员操作. 1