C++Primer12.1.6节练习 12.19题 12.20题

//strBlob.h文件
//类StrBlob的定义
#ifndef STRBLOB_H
#define STRBLOB_H
#include<vector>
#include<string>
#include<initializer_list>
#include<memory>

using namespace::std;

class StrBlobPtr;
class ConstStrBlobPtr;
class StrBlob
{
	friend class StrBlobPtr;
	friend class ConstBlobPtr;
public:
	typedef vector<string>::size_type size_type;
	StrBlob();
	StrBlob(initializer_list<string>il);
	size_type size() const { return data->size(); }
	bool empty() const { return data->empty(); }
    //添加和删除元素
	void push_back(const string& t) { data->push_back(t); }
	void pop_back();
	//元素访问
	string& front();
	string& back();
	const string& front() const;
	const string& back()  const;
	StrBlobPtr begin();
	StrBlobPtr end();
private:
	shared_ptr<vector<string>>data;
	void check(size_type i, const string& msg) const;
};
class StrBlobPtr
{
public:
	StrBlobPtr() :curr(0) { }
	StrBlobPtr(StrBlob &a, size_t sz = 0) :
		wptr(a.data), curr(sz) { }
	string& deref() const;
	StrBlobPtr& incr();  //前缀递增
	size_t cursize() { return curr; }
private:
	//若检查成功,check返回一个指向vector的shared_ptr
	shared_ptr<vector<string>>check(size_t, const string&) const;
	weak_ptr<vector<string>>wptr;
	size_t curr;
};
#endif
//StrBlob.cpp文件
//类的定义
#include "StrBlob.h"
//StrBlob类的实现
StrBlob::StrBlob():data(make_shared<vector<string>>()) { }
StrBlob::StrBlob(initializer_list<string>il) : data(make_shared<vector<string>>(il)) { }
void StrBlob::check(size_type i, const string& msg) const
{
	if (i >= msg.size())
		throw out_of_range(msg);
}
void StrBlob::pop_back()
{
	check(0, "pop_back on empty StrBlob");
	data->pop_back();
}
string& StrBlob::front()
{
	check(0,"front on empty StrBlob");
	return data->front();
}
string& StrBlob::back()
{
	check(0,"back on empty StrBlob");
	return data->back();
}
const string& StrBlob::front()  const
{
	check(0, "front on empty StrBlob");
	return data->front();
}
const string& StrBlob::back()  const //后面的const表示不能修改this指针,前面的const表示返回类型是const
{
	check(0, "back on empty StrBlob");
	return data->back();
}

//StrBlobPtr类的实现
shared_ptr<vector<string>>
StrBlobPtr::check(size_t i, const string &msg) const
{
	auto ret = wptr.lock();
	if (!ret)           //这里表示任何vector的引用都会失败,也就是说有其他shared_ptr指针指向weak_ptr指向的对象都不能进行返回操作
		throw runtime_error("unbound StrBlobPtr");
	if (i >= ret->size())
		throw out_of_range(msg);
	return ret;
}
string& StrBlobPtr::deref() const
{
	auto p = check(curr, "dereference past end");
	return (*p)[curr];  //(*p)是对象所指向的vector
}
StrBlobPtr& StrBlobPtr::incr()
{//前缀递增:返回递增后的对象的引用
 //如果curr已经指向容器的尾后位置,就不能递增它
	check(curr, "increment past end of StrBlobPtr");
	++curr;    //推进当前位置
	return *this;    //指针没有发生变化,只是要引用的值发生了变化

}
StrBlobPtr StrBlob::begin()
{
	return StrBlobPtr(*this, 0);
}
StrBlobPtr StrBlob::end()
{
	auto ret = StrBlobPtr(*this, data->size());
	return ret;
}
void testStrBlobPtr()
{//逐行读入一个文件,将内容存入一个StrBlob中,用一个StrBlob打印出StrBlob中的每个元素
	StrBlob StrB;
	ifstream infile("StrBlob.txt");
	string str;
	string word;
	while(getline(infile,str))
	{
		istringstream istr(str);
		while (istr >> word)
			StrB.push_back(word);
	}
	StrBlobPtr SBPtr(StrB);
	auto i= StrB.begin();
	while(i.cursize()<StrB.size()) //比较索引和总的元素个数
	{
		cout << i.deref() << " ";
		i.incr();//递增索引
	}
	cout << endl;
}
int main()
{
	//测试类与函数
	testStrBlobPtr();
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值