今天把C++primer书上的一道实例实现了一下,理解了shared_ptr的用法
在这个例子中避免了vector和set等的复制,提高了效率
实例功能:
输入:一个file,一个word
输出:该word在此file中出现的次数,以及该word所在的行,一行中若多次出现该word,只计数一次
// smart Pointer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <memory>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <sstream>
using namespace std;
string make_plural(size_t ctr,const string &word, const string &ending)
{
return (ctr==1) ? word : word+ending;//make_plural(wc, "word ", "s ")当输入中文本中
//word数大于一是在word后加s,为words为word的复数!
}
typedef vector<string>::size_type line_no;
class QueryResult;
class TextQuery{
public:
TextQuery(ifstream&);
QueryResult query(const string&) const;
private:
shared_ptr<vector<string>> file;//holds the input file
map<string,shared_ptr<set<line_no>>> wm; // map of each word to the set of the lines in which the word appears
};
TextQuery::TextQuery(ifstream & is):file(new vector<string>){
string text;
while(getline(is,text)){
file->push_back(text);
int n = file->size()-1;//the current line number
istringstream line(text);// separate the line into words
string word;
while(line>>word){
auto &lines = wm[word];//lines is a shared_ptr
if(!lines)// the pointer is null if we first see word
lines.reset(new set<line_no>);//allocate a new set
lines->insert(n);
}
}
}
class QueryResult{
friend ostream& print(ostream&,const QueryResult&);
public:
QueryResult(string s,shared_ptr<set<line_no>> p,shared_ptr<vector<string>> f):sought(s),lines(p),file(f){}
private:
string sought; //word this query represents
shared_ptr<set<line_no>> lines; //lines it's on
shared_ptr<vector<string>> file;//input file
};
QueryResult TextQuery::query(const string & sought) const{
//if we don't find sought,we will return a pointer to this set
static shared_ptr<set<line_no>> nodata(new set<line_no>);
auto loc = wm.find(sought);
if(loc!=wm.end())
return QueryResult(sought,loc->second,file);
else
return QueryResult(sought,nodata,file);
}
ostream& print(ostream & os,const QueryResult &qr){
os<<qr.sought<<" occurs "<<qr.lines->size()<<make_plural(qr.lines->size()," time","s")<<endl;
for(auto num: *qr.lines){
os<<"\t(line "<<num+1<<") "<<*(qr.file->begin()+num)<<endl;
}
return os;
}
void runQueries(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(int argc, char** argv)
{
ifstream file("test.txt");
if(!file){
cout<<"file open failed"<<endl;
return 1;
}
runQueries(file);
return 0;
}