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. Java二维数组

    package com.test; public class Test { public static void main(String[] args) { // TODO Auto-generate ...

  2. 酷盘kanbox获得B轮2000万美元融资

    和阿里近期收购以穷游.虾米为代表的一批小网站相似,酷盘也属于个人用户数量级别庞大,但商业模式并不明晰的企业.目前阿里巴巴集团旗下的阿里云公司拥有自己的云存储业务,其本身既有面向个人用户的产品,也有面向 ...

  3. Spring 3.x企业应用开发实战(14)----事务

    Spring虽然提供了灵活方便的事务管理功能,但这些功能都是基于底层数据库本身的事务处理机制工作的.要深入了解Spring的事务管理和配置,有必要先对数据库事务的基础知识进行学习. 何为数据库事务 “ ...

  4. 手势识别官方教程(4)在挑划或拖动手势后view的滚动用ScrollView和 HorizontalScrollView,自定义用Scroller或OverScroller

    简单滚动用ScrollView和 HorizontalScrollView就够.自定义view时可能要自定义滚动效果,可以使用 Scroller或 OverScroller Animating a S ...

  5. Oracle系列之触发器

    涉及到表的处理请参看原表结构与数据  Oracle建表插数据等等 创建一个触发器,使其可以修改tb_Department表的deptno. create or replace trigger upda ...

  6. keytool 错误: java.io.FileNotFoundException: 拒绝访问

    keytool 错误: java.io.FileNotFoundException: 拒绝访问 打开命令行,切换到D:\AndroidStudioProjects\MyApplication\app目 ...

  7. ACM ICPC Asia Regional 2011 Kuala Lumpur C题

    看了逆波兰表达式之后,发现真是强悍的数据结构,栈的应用怎么感觉一辈子也学不完了呢 后缀表达式即逆波兰表达式,就是将所有的运算符按照一定的等级全部都安排到数字的后面去,实现正确的运算法则. OK,代码要 ...

  8. 如何处理alert、confirm、prompt对话框

    import java.io.File; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.ope ...

  9. Velocity介绍

    Velocity是一个基于Java的模版引擎,它是一个简单并且功能强大的开发工具,你可以非常容易地创建和呈现出.在这个介绍当中,我们希望可以给出一个使用基本Velocity的概述. 使用Velocit ...

  10. Java笔记(三)……基础语法

    关键字 标识符 在程序中自定义的一些名称 由26个英文字母大小写,数字:0-9,符号:_ $组成 定义合法标识符规则: 数字不可以开头. 不可以使用关键字. Java中严格区分大小写. 注意:在起名字 ...