主题:高人请给指点
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
class TextQuery
{
public:
typedef std::vector<std::string>::size_type line_no;
void read_file(std::ifstream &is) {store_file(is);build_map();};
std::set<line_no> run_query(const std::string&) const;
std::string text_line(line_no) const;
private:
void store_file(std::ifstream &);
void build_map();
std::vector<std::string> lines_of_text;
std::map<std::string,std::set<line_no> > word_map;
};
void TextQuery::store_file(std::ifstream &is)
{
string textline;
while(getline(is,textline))
{
lines_of_text.push_back(textline);
}
}
void TextQuery::build_map()
{
for(line_no line_num=0;line_num!=lines_of_text.size();line_num++)
{
// istringstream line(lines_of_text[line_num]);
string word;
while(line>>word)
word_map[word].insert(line_num);
}
}
set<TextQuery::line_no>
TextQuery::run_query(const string &query_word) const
{
map<string,set<line_no> >::const_iterator
loc=word_map.find(query_word);
if(loc==word_map.end())
return set<line_no>();
else
return loc->second;
}
string TextQuery::text_line(line_no line) const
{
if(line<lines_of_text.size())
return lines_of_text[line];
//throw std::out_of_range("line number out of range");
}
int main(int argc, char *argv[])
{
ifstream infile("gj.txt");
if(!infile)
{
cerr<<"can't open \"ww.dat\"for output!"<<endl;
exit(-1);
}
TextQuery tq;
tq.read_file(infile);
while(true)
{
cout<<"Enter word to look for,or q to quit:";
string s;
cin>>s;
if(!cin||s=="q") break;
set<TextQuery::line_no> loc=tq.run_query(s);
print_results(loc,s,tq);
}
return EXIT_SUCCESS;
}
检查不出哪里有错误,请高人给指点一下啊,谢谢先!!
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
class TextQuery
{
public:
typedef std::vector<std::string>::size_type line_no;
void read_file(std::ifstream &is) {store_file(is);build_map();};
std::set<line_no> run_query(const std::string&) const;
std::string text_line(line_no) const;
private:
void store_file(std::ifstream &);
void build_map();
std::vector<std::string> lines_of_text;
std::map<std::string,std::set<line_no> > word_map;
};
void TextQuery::store_file(std::ifstream &is)
{
string textline;
while(getline(is,textline))
{
lines_of_text.push_back(textline);
}
}
void TextQuery::build_map()
{
for(line_no line_num=0;line_num!=lines_of_text.size();line_num++)
{
// istringstream line(lines_of_text[line_num]);
string word;
while(line>>word)
word_map[word].insert(line_num);
}
}
set<TextQuery::line_no>
TextQuery::run_query(const string &query_word) const
{
map<string,set<line_no> >::const_iterator
loc=word_map.find(query_word);
if(loc==word_map.end())
return set<line_no>();
else
return loc->second;
}
string TextQuery::text_line(line_no line) const
{
if(line<lines_of_text.size())
return lines_of_text[line];
//throw std::out_of_range("line number out of range");
}
int main(int argc, char *argv[])
{
ifstream infile("gj.txt");
if(!infile)
{
cerr<<"can't open \"ww.dat\"for output!"<<endl;
exit(-1);
}
TextQuery tq;
tq.read_file(infile);
while(true)
{
cout<<"Enter word to look for,or q to quit:";
string s;
cin>>s;
if(!cin||s=="q") break;
set<TextQuery::line_no> loc=tq.run_query(s);
print_results(loc,s,tq);
}
return EXIT_SUCCESS;
}
检查不出哪里有错误,请高人给指点一下啊,谢谢先!!