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(,"pop_back");
data->pop_back();
} string& StrBlob::back()
{
check(,"back");
return data->back();
} string& StrBlob::front()
{
check(,"front");
return data->front();
} string& StrBlob::back() const
{
check(,"back");
return data->back();
} string& StrBlob::front() const
{
check(,"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() {}
StrBlobPtr(StrBlob &a,size_t sz=):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 ;
}

shared_ptr与weak_ptr的例子的更多相关文章

  1. 关于shared_ptr与weak_ptr的使用(good)

    shared_ptr是带引用计数的智能指针,可以说大部分的情形选择用shared_ptr不会出问题.那么weak_ptr是什么,应该怎么用呢? weak_ptr也是智能指针,但是比较弱,感觉没什么用. ...

  2. c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)

    一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...

  3. c++11 智能指针 unique_ptr、shared_ptr与weak_ptr

    c++11 智能指针 unique_ptr.shared_ptr与weak_ptr C++11中有unique_ptr.shared_ptr与weak_ptr等智能指针(smart pointer), ...

  4. shared_ptr&scoped_ptr&weak_ptr

    [RAII - Resource Acquisition Is Initialization] 获得一个资源的时候,不管这个资源是对象.内存.文件句柄或者其它什么,你都要在一个对象的构造函数中获得它, ...

  5. C++11 新特性之智能指针(shared_ptr, unique_ptr, weak_ptr)

    这是C++11新特性介绍的第五部分,涉及到智能指针的相关内容(shared_ptr, unique_ptr, weak_ptr). shared_ptr shared_ptr 基本用法 shared_ ...

  6. C++ | 再探智能指针(shared_ptr 与 weak_ptr)

    上篇博客我们模拟实现了 auto_ptr 智能指针,可我们说 auto_ptr 是一种有缺陷的智能指针,并且在C++11中就已经被摈弃掉了.那么本章我们就来探索 boost库和C++11中的智能指针以 ...

  7. c++——智能指针学习(shared_ptr和weak_ptr)

    先看一个例子:Stark和Targaryen家族你中有我,我中有你.我们设计以下类企图避免内存泄漏,使得析构函数都能调用到: #include<iostream> #include< ...

  8. C++智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr

    手写代码是理解C++的最好办法,以几个例子说明C++四个智能指针的用法,转载请注明出处. 一.auto_ptr auto_ptr这是C++98标准下的智能指针,现在常常已经被C++标准的其他智能指针取 ...

  9. 聊聊智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr

    本文为转载:https://www.cnblogs.com/zeppelin5/p/10083597.html,对作者有些地方做了修正. 手写代码是理解C++的最好办法,以几个例子说明C++四个智能指 ...

随机推荐

  1. 【Linux安全】chattr命令锁定账户敏感文件

    有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的linux ...

  2. Android 自定义seekbar中,thumb被覆盖掉一部分问题

    (图一)  (图二)    (图三) 做一个自定义的seekbar,更改其背景图片: <com.android.Progress android:id="@+id/focus_seek ...

  3. PO > Create PO时关于汇率问题需要注意的步骤

      为了使得RMB采购的PO在审核时不会提示汇率丢失(如下图),在创建PO时需要注意几个步骤.     1)手动创建PO:在建立PO行之前,应该选择好正确的"地点","币 ...

  4. 先贴出代码C++ 中的单例模式

    先贴出代码,代码后面是讲解 自己编写的单例模式: #include <iostream> #include <stdio.h> #include <string> ...

  5. voucer

    <style type="text/css"> .fieldset_s{border: 1px #dedede solid;padding: 19px; color: ...

  6. 关于ButterKnife 8.1.0使用遇到的问题

    ButterKnife注解方式 和eventbus 差不多 都很好用 @OnClick(R.id.button) void onButtonClick() { //TODO implement Toa ...

  7. BILL.WEI]stimulsoft reports 中panel 妙用

    我们在通过stimulsoft reports做报表,有的时候,我们需要通过合并报表的一些中间列元素,我们就可以用到panel组件 如下图,我们需要合并报表中间项,一般手段达不到要求,只能通过嵌套pa ...

  8. editpuls查找替换通配符

    1  \t    Tab character.         tab符号 2  \n    New line.              新的一行(换行符) 3  .     Matches any ...

  9. linux 开机自动挂载ntfs盘

    1) 查看盘符UUID vellbibi@vell001:~$ sudo blkid [sudo] password for vellbibi: /dev/sda1: UUID="bce9e ...

  10. 获取所有组合算法、获取全排列算法(java)

    转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5574516.html 受到ACM1015的影响,个人感觉,有必要对统计学上的 全组合和全排列 进行一个简单的总结 ...