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. 调色板QPalette类用法详解(附实例、源码)

    http://blog.csdn.net/rl529014/article/details/51589096

  2. Delphi GDI+ Library

    GDI+ LibraryThis library enables GDI+ functionality for Delphi 2009 and later. It differs from other ...

  3. NuGet学习笔记(1)——初识NuGet及快速安装使用

    关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...

  4. poj1160Post Office(DP)

    http://poj.org/problem?id=1160 算水过的吧 四重循环没优化 CZ说爆可过 就爆了 dp[i][j] = min(dp[i][j],dp[i-1][g]-s) 第i个点建在 ...

  5. iconv装换文件编码格式

    最近在mac上编译xml文本文件的时候用vim打开文件汉字总是显示乱码,修改.vimrc,修改iterm编码格式各种方法都使用遍了.最后通过iconv工具将原来的文件编码格式直接转为UTF-8解决掉. ...

  6. [HDU POJ] 逆序数

    HDU 1394 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276 ...

  7. 【C++】命令行Hangman #2015年12月15日 00:20:27

    增加了可以在构造Hangman对象时通过传入参数设定“最大猜测次数”的功能.少量修改.# 2015年12月15日 00:20:22 https://github.com/shalliestera/ha ...

  8. CONTAINING_RECORD 宏

    Windows中提供了一个宏 #define CONTAINING_RECORD (address, type, field ) ((type *)( \ (PCHAR)(address ) - \ ...

  9. 未能找到类型或命名空间“Compare”

    在用vs2012  .net Framework4.5  Mvc3做一个MVCMusicStore 的例子时,遇到这样一个问题 解决办法: 具体原因也不是很清楚,据说是引用Compare做验证时会有二 ...

  10. linux驱动程序之电源管理之Run-time PM 详解(4)

    Run-time PM. 每个device或者bus都会向run-time PM core注册3个callback   struct dev_pm_ops { ... int (*runtime_su ...