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. ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用

    一.运行结果 一.代码1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.c ...

  2. ANDROID_MARS学习笔记_S01原始版_005_ProgressBar

    一.代码 1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  3. Android list1去除list2中的元素

    public static void main(String[] args) { List<String> firList = new ArrayList<String>(); ...

  4. matlab 画图

    先前讲解了简单绘图方法: http://www.cnblogs.com/youxin/p/3859923.html x = 0:pi/100:2*pi; y = sin(x); plot(x,y)下面 ...

  5. lunux下查看文件文件夹大小的命令

    使用ls -lht命令显示当前目录下的所有文件,其中有一列就是显示这个文件的大小.如果要看一个文件夹的大小,可以用du -sh *

  6. MFC窗口重绘

    Invalidate()与 UpdateAllViews()有什么分别 Invalidate()是让程序重画窗口. UpdateAllViews()是在DOC/VIEW结构中, 当一个视图的数据改变后 ...

  7. Native Fullscreen JavaScript API (plus jQuery plugin)

    http://johndyer.name/native-fullscreen-javascript-api-plus-jquery-plugin/ HTML5 <video> is gre ...

  8. HDU4370 0 or 1 最短路

    分析: 1001  (已更新) 显然,题目给的是一个0/1规划模型.解题的关键在于如何看出这个模型的本质.3个条件明显在刻画未知数之间的关系,从图论的角度思考问题,容易得到下面3个结论:1.X12+X ...

  9. [POJ2484]A Funny Game

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4533   Accepted: 2780 Description Alice ...

  10. c++ 派生类向基类转换的可访问性

    对于c++面向对象一直很疑惑,这次决定下功夫把它弄明白 一.派生类和基类之间的类型转换 首先理解,派生类含有基类的所有成分,只不过有些就算在派生类的成员函数也不能访问而已. (1)派生类和基类的自动转 ...