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

//strBlob.h文件
//类StrBlob的定义
#ifndef STRBLOB_H
#define STRBLOB_H
#include
#include
#include
#include

using namespace::std;

class StrBlobPtr;
class ConstStrBlobPtr;
class StrBlob
{
	friend class StrBlobPtr;
	friend class ConstBlobPtr;
public:
	typedef vector::size_type size_type;
	StrBlob();
	StrBlob(initializer_listil);
	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>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>check(size_t, const string&) const;
	weak_ptr>wptr;
	size_t curr;
};
#endif
//StrBlob.cpp文件
//类的定义
#include "StrBlob.h"
//StrBlob类的实现
StrBlob::StrBlob():data(make_shared>()) { }
StrBlob::StrBlob(initializer_listil) : data(make_shared>(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>
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()

你可能感兴趣的:(C++Primer)