c++ primer第五版练习12.20

建立了如下文件:

在这里插入图片描述

  • strblob.h文件
#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
  • strblobptr.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
  • main.cpp文件
#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;
}
  • strblob.cpp文件
#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;
}
  • strblobptr.cpp文件
#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值