boost智能指针与vector

一.以PCL点云库为例子

typedef pcl::PointCloud<PointT> PointCloud;
typedef typename PointCloud::Ptr PointCloudPtr;
typedef typename PointCloud::ConstPtr PointCloudConstPtr;
//使用typedef的原因, 因为shared_ptr是个模板类,typedef起标示作用C++20之前都得声明
typedef boost::shared_ptr <std::vector<int> > IndicesPtr;
typedef boost::shared_ptr <const std::vector<int> > IndicesConstPtr;

这是一个new的vector vector里含int的序列,IndicesPtr拥有所有vector的属性(成员函数,成员变量)

为什么要new

当我们不new的空间,是储存在计算的栈空间上,而这个空间很小,大概几MB,如果一个vector储存的数据结构一个就几MB,那这个vector在栈上声明后,储存几个数据结构后,程序就因为栈空间不足崩溃了。
而new的意思,告诉计算机将这个变量或者数据结构储存到堆空间(内存条)。而这个空间就很大了,小到2g大到上百g。(百g内存大哥喝茶)
所以当我们编程时,需要储存一个数据结构的序列,建议使用智能指针,直接将数据储存到堆空间上。

让我们再看看PCL里的智能指针

typedef PointT PointType;  // Make the template class available from the outside
typedef std::vector<PointT, Eigen::aligned_allocator<PointT> > VectorType;
typedef std::vector<PointCloud<PointT>, Eigen::aligned_allocator<PointCloud<PointT> > > CloudVectorType;
typedef boost::shared_ptr<PointCloud<PointT> > Ptr;
typedef boost::shared_ptr<const PointCloud<PointT> > ConstPtr;
//命名一个含有correspondence的vector为 Correspondences
typedef std::vector< pcl::Correspondence, Eigen::aligned_allocator<pcl::Correspondence> > Correspondences;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值