STL源码剖析——Allocator

本文深入探讨STL中的Allocator,关注auto_ptr和Traits。讲解了迭代器的五种类别,包括input、output、forward、bidirectional和random access iterator,并介绍如何利用不同迭代器类型优化算法。此外,文章还阐述了__type_traits的作用,用于萃取类型特征,以便在算法中进行优化,例如判断是否可以使用内存直接处理操作。
摘要由CSDN通过智能技术生成

auto_ptr

//构造
auto_ptr<string> ps(new string("jjhou");
//获取指针
ps.get();
//重载*, ->
cout << *ps << endl;
cout << ps->size() << endl;

 

Traits

template<class Iterator>
struct iterator_traits
{
    typedef typename Iterator::iterator_category iterator_category;    //迭代器之间的距离
    typedef typename Iterator::value_type value_type;    //迭代器所指对象的类型
    typedef typename Iterator::difference_type difference_type;
    typedef typename Iterator::pointer pointer;
    typedef typename Iterator::reference reference;
};

//指针的特化版本
template<class T>
struct iterator_traits<T*>
{
    typedef random_access_iterator_tag iterator_category;
    typedef T value_type; 
    typedef ptrdiff_t difference_type;
    typedef
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值