概述

Boost.smart_ptr库提供了六种智能指针,除了shared_ptr 和 weak_ptr 以外还包含 scoped_ptr 、scoped_array 、

shared_array 、intrusive_ptr 。

他们的速度与原始指针相差无几,都是异常安全的,并且对于类型T也仅有一个要

求:类型T的析构函数不能抛出异常。

使用时包括头文件:

#include<boost/smart_ptr.hpp>

scoped_ptr

使用方法:

scoped_ptr 的构造函数接受一个类型为T* 的指针p,创建出一个scoped_ptr 类型对象,并在内部保存指针p。p必

须是一个new表达式动态分配的结果,或是个空指针(nullptr)。当scoped_ptr对象的生命期结束时。析构函数会

使用delete操作符自己主动销毁所保存的指针对象,从而正确的回收资源。

scoped_ptr同一时候把拷贝构造函数和赋值操作符都声明为私有的,禁止对智能指针的复制操作,也保证了它管理的指针

不能被转让全部权。

scoped_ptr重载了* 和 ->操作符。能够当普通指针使用。

除此之外没有重载别的运算符。因此不能对其使用++、 --

== 、 != 等运算符。

举例

#include<boost/smart_ptr.hpp>
#include<iostream>
using namespace std;
using namespace boost; int main()
{
scoped_ptr<string> sp(new string("hello world"));
cout<<*sp<<endl;
cout<<sp->size()<<endl;
return 0;
}

scoped_array

使用方法:

构造函数接受的指针必须是 new [ ]的结果。

没有* 、 -> 操作符重载,提供 [ ]运算符,能够与普通数组一样用下标訪问元素

举例:

#include<boost/smart_ptr.hpp>
#include<iostream>
using namespace std;
using namespace boost; int main()
{
scoped_array<int> sp(new int[10]{0,1,2,3,4,5,6,7,8,9});
for(int i=0;i<10;i++)
cout<<sp[i];
return 0;
}

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3hpYW9idXB0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">

shared_ptr

使用方法

shared_ptr实现的是引用型的智能指针。能够被自由的拷贝和赋值。当引用计数为0时,它才会删除被包装的动态分

配的对象。

shared_ptr也能够安全的放到标准容器中,是在STL容器中存储指针的最标准解法。

构造函数

<1>shared_ptr()  无參,创建一个持有空指针的shared_ptr

<2>shared_ptr(T *p)    获得指向T类型指针p的管理权。引用计数+1

<3>shared_ptr(shared_ptr const & r)  从还有一个shared_ptr获得指针的管理权,引用计数+1

<4>shared_ptr(std::auto_ptr<Y> const & r)  从一个auto_ptr获得指针的管理权,引用计数+1,auto_ptr失去管理

<5>operator=  赋值操作符能够用还有一个shared_ptr或auto_ptr获得指针的管理权。其行为同构造函数。

reset函数

将引用计数减一。停止对指针的管理,除非引用计数为0,否则不会发生删除操作。

带參数的reset函数,原指针引用计数减一同一时候改为管理还有一个指针。

检查引用计数函数

unique( )     //唯一时返回true

use_count( ) //返回引用计数个数

注:use_count应该只用于測试,它不提供高效率的操作。而unique()则是可靠的,并且比use_count()快。

其它运算符操作

shared_ptr还支持比較运算,比較基于内部保存的指针 a.get() == b.get()。

shared_ptr还能够使用<<输出内部指针的值。

shared_ptr还能够使用operator<比較大小,相同基于内部保存的指针。因此能够被用于标准关联容器。

类型转换

shared_ptr提供了static_pointer_cast<T>() 、const_pointer_cast<T>() 、dynamic_pointer_cast<T>()。

它们与标准类型转换操作类似,可是返回的类型是shared_ptr。

举例:

#include<boost/smart_ptr.hpp>
#include<iostream>
using namespace std;
using namespace boost; int main()
{
int *ip = new int(10);
boost::shared_ptr<int> sp(ip);
cout<<sp.use_count(); boost::shared_ptr<int> sp2(sp);
cout<<sp.use_count();
cout<<sp2.use_count(); sp2.reset();
cout<<sp.use_count();
cout<<sp.unique();
cout<<sp2.use_count();
return 0;
}

输出:

122110

工厂函数

不仅消除了deletekeyword,也消除了newkeyword,显得对称。

举例:

#include<boost/smart_ptr.hpp>
#include<iostream>
using namespace std;
using namespace boost; int main()
{
boost::shared_ptr<int> sp = make_shared<int>(10);
cout<<sp.use_count()<<endl;
cout<<*sp<<endl; return 0;
}

输出:

1

10

应用于标准容器

一种方法将容器作为shared_ptr管理的对象,如shared_ptr<list<T> >。

还有一种方法将shared_ptr作为容器的元素,如vector<shared_ptr<T> >。由于shared_ptr支持拷贝和比較操作。

举例:

#include <boost/make_shared.hpp>
#include<boost/smart_ptr.hpp>
#include<iostream>
#include<vector>
using namespace std;
using namespace boost; int main()
{ typedef vector<boost::shared_ptr<int> > vs;
vs v(10);
int i = 0;
for (vs::iterator pos = v.begin(); pos != v.end(); ++pos)
{
(*pos) = make_shared<int>(++i);
cout << *(*pos) << ", ";
}
cout << endl;
boost::shared_ptr<int> p = v[9];
*p = 100;
cout << *v[9] << endl; return 0;
}

shared_array

使用方法:

构造函数接受的指针必须是 new [ ]的结果。

没有* 、 -> 操作符重载,提供 [ ]运算符,能够与普通数组一样用下标訪问元素

就像shared_ptr 和 scoped_array的结合

举例:

#include<boost/smart_ptr.hpp>
#include<iostream>
using namespace std;
using namespace boost; int main()
{
int *p = new int[100];
shared_array<int> sa(p);
sa[0] = 1;
cout<<sa[0];
return 0;
}

weak_ptr

使用方法:

weak_ptr是为配合shared_ptr而引入的一种智能指针。它更像是shared_ptr的一个助手而不是智能指针,由于它不

具有普通指针的行为,没有重载*和->。它的最大作用在于协助shared_ptr工作。像旁观者观測资源的使用情况。

能够从一个shared_ptr或一个weak_ptr对象构造,获得资源的观測权。但weak_ptr没有共享资源,因此不用添加引

用计数的值,相同,在析构时也不会导致引用计数降低。

函数

use_count() 能够观測资源的引用计数。

expired()的功能等价于use_count==0,表示被观測的资源已经不存在。

lock()函数看从被观測的shared_ptr获得一个可用的shared_ptr对象,从而操作资源。但当expired()==true时,将

返回一个存储空指针的shared_ptr

举例:

#include<boost/smart_ptr.hpp>
#include<iostream>
using namespace std;
using namespace boost; int main()
{
boost::shared_ptr<int> sp(new int(10));
boost::weak_ptr<int> wp(sp);
cout<<sp.unique()<<endl; if(!wp.expired())
{
boost::shared_ptr<int> sp2 = wp.lock();
*sp2 = 100;
cout<<sp2.use_count()<<endl;
}
cout<<sp.use_count()<<endl;
cout<<*sp<<endl;
return 0;
}

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3hpYW9idXB0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">

intrusive_ptr

使用方法

intrusive_ptr是一个侵入式的引用计数型指针,可用于以下两种情形:

<1>对内存要求很严格,必须与原始指针一样

<2>现存代码已经有了引用计数机制管理的对象

初探boost之smart_ptr库学习笔记的更多相关文章

  1. 初探boost之timer库学习笔记

    timer   使用方法     #include <boost/timer.hpp> #include <iostream> using namespace std; usi ...

  2. 初探boost之progress_display库学习笔记

    progress_display 用途 progress_display能够在控制台上显示程序的运行进度,假设程序运行非常耗费时间,那么它能提供一个友好的用户界 面,不至于让用户在等待中失去耐心,甚至 ...

  3. numpy, matplotlib库学习笔记

    Numpy库学习笔记: 1.array()   创建数组或者转化数组 例如,把列表转化为数组 >>>Np.array([1,2,3,4,5]) Array([1,2,3,4,5]) ...

  4. muduo网络库学习笔记(五) 链接器Connector与监听器Acceptor

    目录 muduo网络库学习笔记(五) 链接器Connector与监听器Acceptor Connector 系统函数connect 处理非阻塞connect的步骤: Connetor时序图 Accep ...

  5. muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制

    目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...

  6. muduo网络库学习笔记(三)TimerQueue定时器队列

    目录 muduo网络库学习笔记(三)TimerQueue定时器队列 Linux中的时间函数 timerfd简单使用介绍 timerfd示例 muduo中对timerfd的封装 TimerQueue的结 ...

  7. C++STL标准库学习笔记(三)multiset

    C++STL标准库学习笔记(三)multiset STL中的平衡二叉树数据结构 前言: 在这个笔记中,我把大多数代码都加了注释,我的一些想法和注解用蓝色字体标记了出来,重点和需要关注的地方用红色字体标 ...

  8. 【python】numpy库和matplotlib库学习笔记

    Numpy库 numpy:科学计算包,支持N维数组运算.处理大型矩阵.成熟的广播函数库.矢量运算.线性代数.傅里叶变换.随机数生成,并可与C++/Fortran语言无缝结合.树莓派Python v3默 ...

  9. C++STL标准库学习笔记(一)sort

    前言: 近来在学习STL标准库,做一份笔记并整理好,方便自己梳理知识.以后查找,也方便他人学习,两全其美,快哉快哉! 这里我会以中国大学慕课上北京大学郭炜老师的<程序设计与算法(一)C语言程序设 ...

随机推荐

  1. mtk GPIO口

    http://blog.csdn.net/mcgrady_tracy/article/details/39320691 mt6582多达168个GPIO口,当然这些GPIO口是复用的,注意lk和Lin ...

  2. Selenium2+python自动化19-单选框和复选框(radiobox、checkbox)【转载】

    本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...

  3. phpstudy无法访问主页,提示You don't have permission to access / on this server解决办法

    1.输入localhost提示:You don't have permission to access / on this server. 新版phpStudy为了安全,取消Apache和nginx列 ...

  4. HDU 6336 子矩阵求和

    Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 ...

  5. HDU 6237.A Simple Stone Game-欧拉函数找素因子 (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)

    A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  6. Linq GroupJoin , Join

    static void Main(string[] args) { List<Person> persons = new List<Person> { }, }, }; Lis ...

  7. Palindrome Permutation -- LeetCode

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  8. [Lydsy1710月赛] 小B的数字

    神TM 又又又又是构造题..... 很简单的化简就是,把2^k[i]都换成k[i] ,然后就可以得出 对于任意的i,k[i] * a[i] >= ∑k[]. 最优的构造肯定是使  k[i] = ...

  9. 解魔方的机器人攻略15 – 安装 Eclipse

    由 动力老男孩 发表于 2009/12/27 17:40:49 在远古时代,程序员们通常用写字板来编写Java程序,然后用Javac.exe和Java.exe来编译和执行.对于NXT来说,对应的命令是 ...

  10. 5.Android之NFC介绍

    NFC简介: Near Field Communication 近场通信,是一种数据传输技术. 与wifi.蓝牙.红外线等数据传输技术的一个主要差异就是有效距离一般不能超过4cm. NFC支持3种工作 ...