文本查询

Query.h

// Query.h: interface for the Query class.
//
//

#if !defined(AFX_QUERY_H__3D990EDB_CAF1_4B2E_A3B6_715714930FCE__INCLUDED_)
#define AFX_QUERY_H__3D990EDB_CAF1_4B2E_A3B6_715714930FCE__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "stdafx.h"

class Query 
{
public:
    Query();
    Query(istream &is);
    virtual ~Query();
    void LoadFile(istream &is);
    void AssembleDirec();
    void query(const string str);
private :
    vector<string> _fileText;
    map<string,set<int> > _strPos;

};

#endif // !defined(AFX_QUERY_H__3D990EDB_CAF1_4B2E_A3B6_715714930FCE__INCLUDED_)

 

 

Query.cpp

// Query.cpp: implementation of the Query class.
//
//

#include "stdafx.h"
#include "Query.h"

//
// Construction/Destruction
//

Query::Query()
{
   
}
Query::Query(istream &is)
{

    LoadFile(is);
}
Query::~Query()
{
   
}

void Query::LoadFile(istream &is)
{

    string str;
    while(getline(is,str))
    {
        _fileText.push_back(str);
    }
}
void Query::AssembleDirec()
{
    vector<string>::iterator it=_fileText.begin();
    _strPos.clear();
    for(int i=0; it != _fileText.end(); it++,i++)
    {
        istringstream iss(*it);
        string str;
        while(iss>>str)
        {
            _strPos[str].insert(i);
        }
    }
   
}

void Query::query(const string str)
{
    AssembleDirec();
    map<string,set<int> >::iterator it=_strPos.find(str);
    if(it != _strPos.end())
    {
        cout<<"seet "<<str<<" as follow"<<endl;
        set<int> poset=it->second;
        for(set<int>::iterator index=poset.begin();index!=poset.end();index++)
        {
            cout<<*index+1<<":"<<_fileText.at(*index)<<endl;
        }

    }
    else
    {
        cout<<str<<"is not exist"<<endl;
    }
}

 

stdafx.h

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__966E6DE8_868A_40FF_AEA8_85D88F70E90E__INCLUDED_)
#define AFX_STDAFX_H__966E6DE8_868A_40FF_AEA8_85D88F70E90E__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#pragma   warning(disable:4786)
#endif // _MSC_VER > 1000

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std ;
// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__966E6DE8_868A_40FF_AEA8_85D88F70E90E__INCLUDED_)

 

stdafx.cpp

// stdafx.cpp : source file that includes just the standard includes
//    TextQuery.pch will be the pre-compiled header
//    stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

QueryText.cpp

 

#include "stdafx.h"
#include "Query.h"
int main(int argc, char* argv[])
{
    cout<<"please input a filename"<<endl;
    string filename,qtext;
    cin>>filename;
    ifstream ifs(filename.c_str());
    Query q(ifs);
    cout<<"please input the string to query "<<endl;
    cin>>qtext;
    q.query(qtext);
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值