c++ Primer 第十二章 动态内存 遗留问题

重点

不要使用new delete手动管理动态内存,而是使用智能指针shared_ptr和unique_ptr

问题

1

12.3节的文本查询类 在定义时为什么使用了

file(new vector<string>)

而不是

make_shared<vector<string>>

这种更安全的方式?

2

最终的print函数当中,为什么可以访问对象的私有成员lines/file??只是因为他们时shared_ptr类型吗

解决

1

实际上是可以的,用make_shared 没有任何问题

2

原来给print设了友元,对不起,我眼瞎。

手写版

#include <iostream>
#include<fstream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <sstream>
#include <cctype>

using namespace  std;

using line_no = vector<string>::size_type;
class QueryResult {
	friend ostream& print(ostream&, const QueryResult&);
public:
	QueryResult(string s, shared_ptr<set<line_no>> p, shared_ptr<vector<string>> f) :target(s), lines(p), file(f) {}
private:
	string target;
	shared_ptr<set<line_no>> lines;
	shared_ptr<vector<string>> file;
};

class TextQuery {
public:
	
	TextQuery(ifstream&);
	QueryResult query(const string& word);
private:
	shared_ptr<vector<string>> file;
	map<string, shared_ptr<set<line_no>>> word2line;
};


TextQuery::TextQuery(ifstream& ifs)
{
	file = make_shared <vector<string>>();
	string line;
	while (getline(ifs, line)) {
		file->push_back(line);
		int n = file->size() - 1;//行号
		istringstream lineStream(line);
		string word;
		while (lineStream >> word) {
			string noPuncWord;
			for (auto c : word) {
				if (c < 0 || c > 255) continue;
				else if (!ispunct(c)) noPuncWord += c;
			}
			shared_ptr<set<line_no>> &wordLines = word2line[noPuncWord];
			if (!wordLines) {
			//	wordLines.reset(make_shared<set<line_no>>);//这里报错无法对模板实例化
				wordLines = make_shared<set<line_no>>();//可以使用
			//	wordLines.reset(new set<line_no>);
			}
			wordLines->insert(n);

		}
		
	}
}

QueryResult TextQuery::query(const string& word)
{
	//不可取,因为下标运算符会将word添加到map当中
	//QueryResult result(word, word2line[word], file);
	auto lineNum = word2line.find(word);//这里lineNum的类型应该是对应的迭代器
	static shared_ptr<set<line_no>> nodata = make_shared<set<line_no>>();
	if (lineNum != word2line.end()) {
		return QueryResult(word, lineNum->second, file);
	}
	else return QueryResult(word, nodata, file);

	 
}

ostream& print(ostream& os, const QueryResult& result)
{
	os << result.target << " occurs " << result.lines->size() << " " << "times" << endl;
	for (auto lineNum : *result.lines)
		os << "\t(lines" << lineNum + 1 << ")" << *(result.file->begin() + lineNum) << 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 agrc, char* argv)
{
	ifstream infile("F:\\Harry\\harry+potter5.txt");
	if (infile)
		runQueries(infile);
	else
		cout << "open error" << endl;
	infile.close();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值