#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <fstream>
#include <set>
#include <map>
#include <sstream>
class QueryResult;
std::string make_plural(size_t ctr, const std::string& word, const std::string& ending);
class TextQuery {
public:
using line_no = std::vector<std::string>::size_type;
TextQuery(std::ifstream&);
QueryResult query(const std::string&) const;
private:
std::shared_ptr<std::vector<std::string>> file; // 输入文件
std::map<std::string, std::shared_ptr<std::set<line_no>>> wm;
};
class QueryResult {
friend std::ostream& print(std::ostream&, const QueryResult&);
public:
QueryResult(std::string s, std::shared_ptr<std::set<TextQuery::line_no>> p,
std::shared_ptr<std::vector<std::string>> f) : sought(s), lines(p), file(f) {}
private:
std::string sought; // 查询的词
std::shared_ptr<std::set<TextQuery::line_no>> lines;
std::shared_ptr<std::vector<std::string>> file;
};
// 读入行号并建立单词到行号的映射
TextQuery::TextQuery(std::ifstream& is) : file(new std::vector<std::string>) {
std::string text;
while (getline(is, text)) {
file->push_back(text); // 保存此行文本
int n = file->size() - 1; // 行号
std::istringstream line(text);
std::string word;
while (line >> word) {
auto& lines = wm[word];
if (lines == nullptr) {
lines.reset(new std::set<line_no>);
}
lines->insert(n);
}
}
}
QueryResult TextQuery::query(const std::string& sought) const {
// 如果未找到,返回一个空的 set
static std::shared_ptr<std::set<TextQuery::line_no>>
nodata(new std::set<TextQuery::line_no>);
auto loc = wm.find(sought);
if (loc != wm.end()) {
return QueryResult(sought, loc->second, file);
} else {
return QueryResult(sought, nodata, file);
}
}
std::ostream& print(std::ostream& os, const QueryResult& qr) {
os << qr.sought << " occurs" << qr.lines->size() << " "
<< make_plural(qr.lines->size(), "time", "s") << std::endl;
for (auto num : *qr.lines) {
os << "(line " << num + 1 << ") " << *(qr.file->begin() + num) << std::endl;
}
return os;
}
std::string make_plural(size_t ctr, const std::string& word, const std::string& ending) {
return ctr > 1 ? word + ending : word;
}
void runQueries(std::ifstream& infile) {
TextQuery tq(infile);
while (true) {
std::cout << "enter word to look for, or q to quit" << std::endl;
std::string s;
if (!(std::cin >> s) || s == "q") {
break;
}
print(std::cout, tq.query(s)) << std::endl;
}
}
int main() {
std::string fileName;
std::cin >> fileName;
std::ifstream file;
file.open(fileName);
if (file.is_open()) {
runQueries(file);
file.close();
} else {
std::cout << "Failed to open the file." << std::endl;
}
return 0;
}
标签:std,文件,QueryResult,string,file,const,include
From: https://www.cnblogs.com/hacker-dvd/p/17471711.html