使用智能指针(share_ptr)的一些想法
以下是c++primer 的单词查询程序,书中的程序用到了智能指针,在学习的过程中,因不明白为何要用智能指针,于是写了一个不用智能指针的版本,
最终,通过两个程序的区别,理解了使用智能指针的原因:
#include<map>
#include<vector>
#include<string>
#include<fstream>
#include<sstream>
#include<set>
#include<iostream>
#include<memory>
using namespace std;
using line_no = vector<string>::size_type;
class QueryResult;
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;
shared_ptr<set<line_no>> lines; //出现的行号
shared_ptr<vector<string>> file;
};
class TextQuery
//map<string,set<int>> vector
{
public:
using line_no = vector<string>::size_type;
TextQuery(ifstream&);
QueryResult query(const string&)const;
private:
shared_ptr<vector<string>> file;
map<string, shared_ptr<set<line_no>>> wm;
};
QueryResult TextQuery::query(const string& sought) const
{
static shared_ptr<set<line_no>> nodata(new set<line_no>);
auto loc = wm.find(sought);
if (loc == wm.end())
{
return QueryResult(sought, nodata, file);
}
else
{
return QueryResult(sought, loc->second, file);
}
}
TextQuery::TextQuery(ifstream& ifs):file(new vector<string>)
{
string text;
while (getline(ifs,text))
{
file->push_back(text);
int n = file->size() - 1;
istringstream line(text);
string word;
while (line>>word)
{
auto& lines = wm;
if (!lines)
{
lines.reset(new set<line_no>);
}
lines->insert(n);
}
}
}
ostream& print(ostream&os, const QueryResult&qr)
{
//如果找到了单词,打印出现次数和所有出现位置
os << qr.sought << " occurs " << qr.lines->size() << " " << "times" << endl;
//打印单词出现的每一行
for (auto num : *qr.lines)
{
os << "\t(line " << num + 1 << ") " << *(qr.file->begin() + num) << endl;
}
return os;
}
void runQueries(ifstream& ifs)
{
TextQuery tq(ifs);
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 ifs(argv);
if (ifs)
{
runQueries(ifs);
}
else
{
cerr << "ifs error." << endl;
}
return 0;
}
不使用智能指针的版本
不使用智能指针的版本
#include<vector>
#include<string>
#include<fstream>
#include<map>
#include<set>
#include<iostream>
#include<sstream>
using namespace std;
using line_no = vector<string>::size_type;
class QueryResult
{
friend ostream& print(ostream&,const QueryResult&);
public:
QueryResult(string s, set<line_no> l, vector<string> f) :
sought(s),lines(l),file(f){ };
private:
string sought;
set<line_no> lines;
vector<string> file;
};
class TextQuery
{
public:
TextQuery(ifstream&);
QueryResult query(const string&)const;
private:
vector<string> file;
map<string, set<line_no>> wm;
};
TextQuery::TextQuery(ifstream& ifs)
{
string text;
while (getline(ifs,text))
{
file.push_back(text);
int n = file.size() - 1;
istringstream line(text);
string word;
while (line>>word)
{
auto &lines = wm;
lines.insert(n);
}
}
}
QueryResult TextQuery::query(const string& sought) const
{
auto loc = wm.find(sought);
if (loc == wm.end()) //如果没有找到给定的单词
{
//
set<line_no> initSet{};
return QueryResult(sought, initSet, file);
}
else
{
return QueryResult(sought, loc->second, file);
}
}
ostream& print(ostream& os, const QueryResult&qr)
{
os << qr.sought << " occurs " << qr.lines.size() << " " << "times" << endl;
for (auto num : qr.lines)
{
os << "\t(line " << num + 1 << ") " << *(qr.file.begin() + num) << endl;
}
return os;
}
void runQueries(ifstream& ifs)
{
TextQuery tq(ifs);
while (true)
{
cout << "Enter the word to look for, or Q to quit :" << endl;
string s;
if (!(cin >> s) || s == "q")break;
print(cout, tq.query(s)) << endl;
}
}
int main(int argc,char*argv[])
{
ifstream ifs(argv);
if (ifs)
{
runQueries(ifs);
}
else
{
cerr << "ifs error." << endl;
}
return 0;
}
核心在于query函数返回的QueryResult对象是通过拷贝初始化,还是用指针来初始化。
页:
[1]