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