建立了如下文件:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190301170912754.png)
#ifndef STRBLOB_H
#define STRBLOB_H
#include <string>
#include <vector>
#include <initializer_list>
#include <memory>
using namespace std;
//伴随指针类前置声明
class StrBlobPtr;
//容器类定义
class StrBlob {
public:
//友元声明
friend class StrBlobPtr;
using size_type = vector<string>::size_type;
StrBlob();
StrBlob(initializer_list<string> lst);
//size()
size_type size() const;
//empty()
bool empty() const;
//push_back()
void push_back(string &s);
//pop_back()
void pop_back() const;
//front
string& front() const;
//back()
string& back() const;
// StrBlobPtr begin();
StrBlobPtr end();
private:
//指向vector的一个智能指针
shared_ptr<vector<string>> data;
//check函数
void check(size_type i, const string &msg) const;
};
#endif // STRBLOB_H
#ifndef STRBLOBPTR_H
#define STRBLOBPTR_H
#include "strblob.h"
#include <memory>
#include <vector>
#include <string>
using namespace std;
//伴随指针类定义
class StrBlobPtr {
public:
StrBlobPtr ();
StrBlobPtr (StrBlob &a, size_t sz);
//解引用函数
string& defen() const;
//前缀递增函数
StrBlobPtr& incr();
size_t re_curr();
private:
//check函数
shared_ptr<vector<string>> check(size_t sz, const string& msg) const;
//指向vector的一个weak_ptr类型的智能指针
weak_ptr<vector<string>> wptr;
//当前下标
size_t curr;
};
#endif // STRBLOBPTR_H
#include <iostream>
#include "strblobptr.h"//包含头文件
#include "strblob.h"
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream in("str.txt");
StrBlob m_sb;
string word;
//m_sb就是一个容器类对象,存放着读入的每一行数据
while(getline(in, word))
m_sb.push_back(word);
//m_sbp是指针类对象那个,根据构造函数,m_sbp将指向m_sb中的第一个元素,即begin()迭代器
StrBlobPtr m_sbp(m_sb, 0);
//打印
for(StrBlobPtr e = m_sb.end();
m_sbp.re_curr() != e.re_curr();m_sbp.incr())
cout<<m_sbp.defen()<<endl;
return 0;
}
#include "strblob.h"
//包含strblobptr.h
#include "strblobptr.h"
StrBlob::StrBlob():data(make_shared<vector<string>>(0)){}
StrBlob::StrBlob(initializer_list<string> lst):
data(make_shared<vector<string>>(lst)){}
StrBlob::size_type StrBlob::size() const {return data->size();}
bool StrBlob::empty() const {return data->empty();}
void StrBlob::push_back(string &s) {data->push_back(s);}
void StrBlob::pop_back() const{
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
string &StrBlob::front() const {
check(0, "front on empty StrBlob");
return data->front();
}
string &StrBlob::back() const {
check(0, "back on empty StrBlob");
return data->back();
}
void StrBlob::check(StrBlob::size_type i, const string &msg) const{
//为空或者超出范围
if(i >= data->size())
throw out_of_range(msg);
}
//StrBlobPtr StrBlob::begin(){
// return StrBlobPtr(*this, 0);
//}
//迭代器end
StrBlobPtr StrBlob::end(){
auto ret = StrBlobPtr(*this, data->size());
return ret;
}
#include "strblobptr.h"
StrBlobPtr::StrBlobPtr():curr(0) {}
StrBlobPtr::StrBlobPtr(StrBlob &a, size_t sz):
wptr(a.data), curr(sz) {}
string &StrBlobPtr::defen() const
{
auto p = check(curr, "dereference past end");
return (*p)[curr];
}
StrBlobPtr &StrBlobPtr::incr()
{
check(curr, "increment past end of StrBlobPtr");
++curr;
return *this;
}
size_t StrBlobPtr::re_curr(){return curr;}
shared_ptr<vector<string> > StrBlobPtr::check(size_t sz, const string &msg) const
{
auto ret = wptr.lock();
if(!ret)
throw runtime_error("unbound StrBlobPtr");
if(sz >= ret->size())
throw out_of_range(msg);
return ret;
}