使用shared_ptr实例

通过C++primer书籍的实例,深入理解并实践了shared_ptr的使用,该实例优化了vector和set的复制,提升效率。实现功能为:读取文件,统计并输出给定单词在文件中出现的次数及所在行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天把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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值