最近打算学习c++11 中智能指针的使用,写了一个文本查询程序。功能有读入一个“.txt”文件,在其中找单词“london”,具体格式见图:
//TextQuery.cpp
#include "TextQuery.h"
#include<iostream>
#include<fstream>
using namespace std;
void runQueries(std::ifstream& infile) {
TextQuery tq(infile);
while (true) {
cout << "enter word to look for,or q to quit:";
string s;
if (!(cin >> s) || s == "q") break;
print(cout,tq.query(s))<<endl;
}
}
int main() {
ifstream file("1.txt");
runQueries(file);
return 0;
}
//TextQuery.h
#include<string>
#include<fstream>
#include<map>
#include<set>
#include<vector>
#include<iostream>
#include<sstream>
#include<memory>
#include<cstdio>
#include<algorithm>
#include<iterator>
using namespace std;
class QueryResult;
class TextQuery {
public:
using LineNo = vector<string>::size_type;
TextQuery(std::ifstream&);
QueryResult query(const string&) const;
private:
shared_ptr<vector<string>> input;
std::map<string, shared_ptr<std::set<LineNo>>> result;
};
class QueryResult {
public:
friend std::ostream& print(std::ostream&,const QueryResult&);
public:
QueryResult(const string& s, shared_ptr<std::set<TextQuery::LineNo>> p,shared_ptr<vector<string>> f):sought(s),lines(p),file(f)
{
}
private:
string sought;
shared_ptr<std::set<TextQuery::LineNo>> lines;
shared_ptr<vector<string>> file;
};
std::ostream& print(std::ostream&,const QueryResult&);
TextQuery::TextQuery(std::ifstream& ifs) :input(new vector<string>) {
string text;
while (getline(ifs, text)) {
input->push_back(text);
int n = input->size() - 1;
istringstream line(text);
string word;
while (line >> word) {
auto &lines = result[word];
if (!lines)
lines.reset(new set<LineNo>);
lines->insert(n);
}
}
}
QueryResult TextQuery::query(const string& sought) const {
static shared_ptr<std::set<LineNo>> nodate(new std::set<LineNo>);
auto loc = result.find(sought);
if (loc == result.end())
return QueryResult(sought, nodate, input);
else
return QueryResult(sought, loc->second, input);
}
std::ostream& print(std::ostream& out, const QueryResult& qr) {
out << qr.sought << " occurs " <<qr.lines->size() << (qr.lines->size() > 1 ? " times" : " time") << std::endl;
for (auto num : *qr.lines)
out << "\t(line" << num + 1 << ") " << *(qr.file->begin()+num) << std::endl;
return out;
}
我的“ 1.txt”:
Londoners are under starter's orders as the city gets ready for the Olympic Games, which will begin one year today.
To mark the start of the 366-day countdown (2012 is a leap year), special events are planned for today.
The design of the Olympic medals will be unveiled tonight in a live ceremony from Trafalgar Square.
Over at the brand new Aquatics Centre, Britain's star diver Tom Daley is going to perform an official launch dive into the Olympic pool.
With this building, the organisers have attempted to give London a landmark to rival Beijing's Water Cube from 2008.
It was designed by the prestigious architect Zaha Hadid and has a wave-like roof that is 160 metres long.
Today's special events are designed to arouse interest in the Olympics around the world and to encourage British fans too.
Many failed to get Olympic tickets in the recent sales process.
According to a new survey for the BBC, 53% of Londoners think the process was "not fair".
But the same survey found support is growing for London 2012. Of the 1,000 people surveyed, 73% said they backed the Games - up from 69% in 2006.
Olympics minister Hugh Robertson said: "We are under budget and ahead of time and as a nation we have a reputation of really getting behind these big events."
运行结果:
enter word to look for,or q to quit:London
London occurs 2 times
(line5) With this building, the organisers have attempted to give London a landmark to rival Beijing's Water Cube from 2008.
(line10) But the same survey found support is growing for London 2012. Of the 1,000 people surveyed, 73% said they backed the Games - up from 69% in 2006.
enter word to look for,or q to quit: