shared_ptr与weak_ptr的例子

简介: 12.20 编写程序,逐行读入一个输入文件,将内容存入一个StrBlob中,用一个StrBlobPtr打印出StrBlob的每个元素。 StrBlob.h #ifndef STRBLOB_H #define STRBLOB_H #include #include #include ...

12.20 编写程序,逐行读入一个输入文件,将内容存入一个StrBlob中,用一个StrBlobPtr打印出StrBlob的每个元素。

StrBlob.h

#ifndef STRBLOB_H
#define STRBLOB_H
#include<iostream>
#include<string>
#include<vector>
#include<memory>
using namespace std;
class StrBlobPtr;
class StrBlob
{
friend class StrBlobPtr;
public:
    typedef string::size_type size_type;
    //构造函数
    StrBlob();
    explicit 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();
    string& front() const;
    string& back() const;
    size_type count()
    {
        return data.use_count();
    }
    StrBlobPtr begin();
    StrBlobPtr end();
private:
    shared_ptr<vector<string>> data;
    void check(size_type i,const string msg) const;
};
#endif // STRBLOB_H

StrBlob.cpp

#include"StrBlob.h"
#include"StrBlobPtr.h"
StrBlob::StrBlob():data(make_shared<vector<string>>())
{
}

StrBlob::StrBlob(initializer_list<string> il):data(make_shared<vector<string>>(il))
{
}

void StrBlob::pop_back()
{
    check(0,"pop_back");
    data->pop_back();
}

string& StrBlob::back()
{
    check(0,"back");
    return data->back();
}

string& StrBlob::front()
{
    check(0,"front");
    return data->front();
}

string& StrBlob::back() const
{
    check(0,"back");
    return data->back();
}

string& StrBlob::front() const
{
    check(0,"front");
    return data->front();
}
void StrBlob::check(size_type i, const string msg) const
{
    if(i>=data->size())
        throw out_of_range(msg);
}

StrBlobPtr StrBlob::begin()
{
    return StrBlobPtr(*this);
}

StrBlobPtr StrBlob::end()
{
    return StrBlobPtr(*this,data->size());
}

StrBlobPtr.h

#ifndef STRBLOBPTR_H
#define STRBLOBPTR_H
#include<string>
#include<vector>
#include<memory>
using namespace std;

class StrBlobPtr
{
public:
    StrBlobPtr():curr(0) {}
    StrBlobPtr(StrBlob &a,size_t sz=0):wptr(a.data),curr(sz) {}

    string& deref() const;
    StrBlobPtr& incr();
private:
    shared_ptr<vector<string>> check(size_t,const string &) const;
    weak_ptr<vector<string>> wptr;
    size_t curr;
};
#endif

StrBlobPtr.cpp

#include"StrBlob.h"
#include"StrBlobPtr.h"

shared_ptr<vector<string>> StrBlobPtr::check(size_t i, const string& msg) const
{
    shared_ptr<vector<string>> ret=wptr.lock();
    if(!ret)
        throw runtime_error("unbound StrBlobPtr");
    if(i>=ret->size())
        throw out_of_range(msg);
    return ret;
}

string& StrBlobPtr::deref() const
{
    auto ret=check(curr,"deference");
    return (*ret)[curr];
}

StrBlobPtr& StrBlobPtr::incr()
{
    check(curr,"increment");
    ++curr;
    return *this;
}

useStrBlob.cpp

#include"StrBlob.h"
#include"StrBlobPtr.h"
#include<fstream>

int main()
{
    ifstream in("1.txt");
    StrBlob Str;
    StrBlobPtr StrP(Str);
    string tmp;
    while(getline(in,tmp))
    {
        Str.push_back(tmp);
    }
    size_t l=Str.size();
    while(l)
    {
        cout<<StrP.deref()<<endl;
        StrP.incr();
        --l;
    }
    return 0;
}

 

相关文章
|
1月前
|
存储 安全 程序员
【C++ 包装器类 智能指针】完全教程:std::unique_ptr、std::shared_ptr、std::weak_ptr的用法解析与优化 — 初学者至进阶指南
【C++ 包装器类 智能指针】完全教程:std::unique_ptr、std::shared_ptr、std::weak_ptr的用法解析与优化 — 初学者至进阶指南
69 0
|
3月前
|
安全 编译器 C++
智能指针shared_ptr、unique_ptr、weak_ptr
智能指针shared_ptr、unique_ptr、weak_ptr
84 0
|
3月前
|
C++
C++智能指针shared_ptr
C++智能指针shared_ptr
25 0
|
3月前
shared_ptr循环引用问题以及解决方法
shared_ptr循环引用问题以及解决方法
40 0
|
4月前
|
安全 编译器 C++
[C++] 智能指针(shared_ptr、unique_ptr)
[C++] 智能指针(shared_ptr、unique_ptr)
27 1
shared_ptr产生的交叉引用问题
智能指针方便了程序设计人员对于内存的使用,但是也带来了很多问题。如交叉引用问题、线程安全问题等,本文将详细介绍交叉引用问题。
|
4月前
C++11 shared_ptr智能指针
C++11 shared_ptr智能指针
32 0
|
10月前
|
编译器 C++
shared_ptr 和 unique_ptr 深入探秘
shared_ptr 和 unique_ptr 深入探秘
95 0
|
10月前
|
程序员 C++
C++11之智能指针(unique_ptr、shared_ptr、weak_ptr、auto_ptr)浅谈内存管理
C++11之智能指针(unique_ptr、shared_ptr、weak_ptr、auto_ptr)浅谈内存管理
85 0
|
12月前
|
安全 Linux 开发工具
C++11 智能指针之shared_ptr<void>
基于Alexa的全链路智能语音SDK基于C++实现了跨平台特性,跑通了Android、Mac、Linux等设备,在兼容iOS时发现iOS未提供音频采集和播放的C++接口,所以需要改造SDK,允许SDK初始化时注入外部的采集器和播放器实现类,同时SDK中的Android播放器是基于ffmpeg解码 + opensl实现,但是考虑到包体积的问题,准备也基于这个接口在外部实现基于Android硬件解码的播放器。
281 0