迭代器(iterator) 与traits编程

迭代器(iterator) 与traits编程

迭代器(iterators)是一种抽象的设计概念,现实程序语言中并没有直接对应于这个概念的实物 Design Patterns一书提供有23个设计模式(design patterns )的完整描述,其中iterator模式定义如下:提供一种方法,使之能够依序巡访某个聚合物(容器)所含的各个元素,而又无需暴露该聚合物的内部表述方式。

不论是泛型思维或STL的实际运用,迭代器 iterator都扮演着重要的角色。STL的中心思想在于:将数据容器(container)和算法(algorithms)分开,彼此独立设计,最后再以一帖胶着剂将它们撮合在一起。容器和算法的泛型化,从技术角度来看并不困难,C++的class templates和function templates可分别达成目标。如何设计出两者之间的良好胶着剂,才是大难题。

迭代器(iterator)是一种smart pointer

其实迭代器更像一个指针,需要在容器内部移动,取值,赋值。

因此,迭代器最重要的编程工作就是对operator*和operator->进行重载(overloading)工作。这一点,C++标准程序库有一个auto_ptr。关于可供我们参考。任何一本详尽的c++语法书籍都应该谈到auto_ptr(如果没有,扔了它⑧),这是一个用来包装原生指针(native pointer)的对象,声名狼藉的内存漏洞(memory leak)问题可藉此获得解决。auto_ptr用法如下,和原生指针一模一样:

template <class _Tp> class auto_ptr{
private:
  _Tp* _M_ptr;

public:
  typedef _Tp element_type;

  explicit auto_ptr(_Tp* __p = 0) __STL_NOTHROW : _M_ptr(__p) {}
  auto_ptr(auto_ptr& __a) __STL_NOTHROW : _M_ptr(__a.release()) {}

#ifdef __STL_MEMBER_TEMPLATES
  template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) __STL_NOTHROW
    : _M_ptr(__a.release()) {}
#endif /* __STL_MEMBER_TEMPLATES */

  auto_ptr& operator=(auto_ptr& __a) __STL_NOTHROW {
    if (&__a != this) {
      delete _M_ptr;
      _M_ptr = __a.release();
    }
    return *this;
  }

#ifdef __STL_MEMBER_TEMPLATES
  template <class _Tp1>
  auto_ptr& operator=(auto_ptr<_Tp1>& __a) __STL_NOTHROW {
    if (__a.get() != this->get()) {
      delete _M_ptr;
      _M_ptr = __a.release();
    }
    return *this;
  }
#endif /* __STL_MEMBER_TEMPLATES */

  ~auto_ptr() __STL_NOTHROW { delete _M_ptr; }

  _Tp& operator*() const __STL_NOTHROW {
    return *_M_ptr;
  }
  _Tp* operator->() const __STL_NOTHROW {
    return _M_ptr;
  }
  _Tp* get() const __STL_NOTHROW {
    return _M_ptr;
  }
  _Tp* release() __STL_NOTHROW {
    _Tp* __tmp = _M_ptr;
    _M_ptr = 0;
    return __tmp;
  }
  void reset(_Tp* __p = 0) __STL_NOTHROW {
    if (__p != _M_ptr) {
      delete _M_ptr;
      _M_ptr = __p;
    }
  }

  // According to the C++ standard, these conversions are required.  Most
  // present-day compilers, however, do not enforce that requirement---and, 
  // in fact, most present-day compilers do not support the language 
  // features that these conversions rely on.
  
#if defined(__SGI_STL_USE_AUTO_PTR_CONVERSIONS) && \
    defined(__STL_MEMBER_TEMPLATES)

public:
  auto_ptr(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW
    : _M_ptr(__ref._M_ptr) {}

  auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW {
    if (__ref._M_ptr != this->get()) {
      delete _M_ptr;
      _M_ptr = __ref._M_ptr;
    }
    return *this;
  }

  template <class _Tp1> operator auto_ptr_ref<_Tp1>() __STL_NOTHROW 
    { return auto_ptr_ref<_Tp1>(this->release()); }
  template <class _Tp1> operator auto_ptr<_Tp1>() __STL_NOTHROW
    { return auto_ptr<_Tp1>(this->release()); }

#endif /* auto ptr conversions && member templates */
}

迭代器相应型别

在使用迭代器的时候我们经常需要知道所指对象的类型。但是C++不像java不能获得类型名称。

解决办法是:利用function template的参数推导(argument deducation)机制。

我们以func()为对外接口,却把实际操作全部置于func_impl()之中。由于function_imp() 是一个function template,一旦被调用,编译器会自动进行template参数推导。于是导出型别T,顺利解决了问题.

Traits编程技法一STL源代码门钥

迭代器所指对象的型别技巧虽然可用于value type,,称为该迭代器的value type.上述的参数型别推导却非全面可用:万一value type回值,就束手无策了,毕竟函数的“template参数推导机制”必须用于函数的传推而导之的只是参数,无法推倒函数返回值类型。

解决方法之一为声明内嵌类型。

在func前面必须加上typename,这里通知编译器此处为类型。(否则编译器可能认为是一个member function 或者 member data)才能编译通过。

还有一个问题,并不是所有的迭代器都是class type.因为原生指针是一种迭代器,但是其不是类类型。无法定义内嵌类型。这时候使用偏特化处理。

Partial Specialization

大致的意义是:如果class template拥有一个以上的template参数,我们可以针对其中某个(或数个,但非全部)template参数进行特化工作。换句话说,我们可以在泛化设计中提供一个特化版本(也就是将泛化版本中的某些template参数赋予明确的指定)。

偏特化的意思是对泛化版本进行修饰限定,其本质是窄化的模板。



template<typename T>
  class C{};//接受T为任何类型

template<typename T>
  class C<T*>{};//特化为T为原生指针的情况

有了这项利器,我们便可以解决前述“内嵌型别”未能解决的问题。先前的问题是,原生指针并非class,因此无法为它们定义内嵌型别。现在,我们可以针对“迭代器之template参数为指针”者,设计特化版的迭代器。

提高警觉,我们进人关键地带了!下面这个class template专门用来“萃取”迭代器的特性,而value type正是迭代器的特性之一;

template <class _Iterator>
struct iterator_traits {    
 typedef typename _Iterator::value_type        value_type;
 };

这个所谓的traits,其意义是,如果工定义有自己的value type,那么通过这个traits的作用,萃取出来的value_type就是I::value_type。换句话说,如果工定义有自己的value_type,先前那个func可以改写成这样:.


template <class I>
  typename iterator_traits<I>::value_type //在class I中必须定义了 value_type
    func(I ite){
      return *ite;
    }

增加这一层是为了解决原生指针的问题:


template <class _Iterator>
struct iterator_traits <T*>{    
 typedef T value_type;
 };
template <class _Iterator>
struct iterator_traits <const T*>{  
 typedef T value_type;
 };

这样做原生指针int* 传入进来,也可以萃取出value_type.int* ->int

例子:

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 _Tp> //版本二 偏特化版本
struct iterator_traits<_Tp*> {
  typedef random_access_iterator_tag iterator_category;
  typedef _Tp                         value_type;
  typedef ptrdiff_t                   difference_type;
  typedef _Tp*                        pointer;
  typedef _Tp&                        reference;
};


//deque 通过版本一
template <class _Tp, class _Ref, class _Ptr, size_t __bufsiz>
struct _Deque_iterator {
  typedef _Deque_iterator<_Tp,_Tp&,_Tp*,__bufsiz>             iterator;
  typedef _Deque_iterator<_Tp,const _Tp&,const _Tp*,__bufsiz> const_iterator;
  
  typedef random_access_iterator_tag iterator_category;
  typedef _Tp value_type;
  typedef _Ptr pointer;
  typedef _Ref reference;
  typedef size_t size_type;
  typedef ptrdiff_t difference_type;
...
};

//vector 使用原生指针
template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
class vector : protected _Vector_base<_Tp, _Alloc> 
{
private:
  typedef _Vector_base<_Tp, _Alloc> _Base;
public:
  typedef _Tp value_type;
  typedef value_type* pointer;//指向value_type 类型
  typedef const value_type* const_pointer;
  typedef value_type* iterator;//指针类型 和pointer定义一致 不是类类型
  typedef const value_type* const_iterator;
  typedef value_type& reference;
  typedef const value_type& const_reference;
  typedef size_t size_type;
  typedef ptrdiff_t difference_type;
  ...
};
//在使用如下函数的时候就会展开
template <class I>
  typename iterator_traits<I>::value_type //在class I中必须定义了 value_type
    func(I ite){
      return *ite;
    }

迭代器类型:

  1. value type迭代器所指对象的型别。

  2. difference type用来表示两个迭代器之间的距离,因此它也可以用来表示一个容器的最大容量,因为对于连续空间的容器而言,头尾之间的距离就是其最大容量。如果一个泛型算法提供计数功能,例如STL的count ( ),其传回值就必须使用迭器的difference type:

    
    template <class _InputIter, class _Tp>
    typename iterator_traits<_InputIter>::difference_type
    count(_InputIter __first, _InputIter __last, const _Tp& __value) {
      typename iterator_traits<_InputIter>::difference_type __n = 0;
      for ( ; __first != __last; ++__first)
        if (*__first == __value)
          ++__n;
      return __n;
    }
  3. reference type

    从“迭代器所指之物的内容是否允许改变”的角度观之,迭代器分为两种:不允许改变 “所指对象之内容”者,称为constant iterators例如const int* pic;允许改变“所指对象之内容”者,称为mutable iterators,如int * pi.

    C++中传回左值使用by reference..如果其value type是T,那么p的型别不应该是T,应该是T&。将此道理扩充,如果p是一个constant iterators,其value type是T3那么p的型别不应该是Const T,而应该是constT&。这里所讨论的*p的型别,即所谓的reference type。实现细节将在下一小节一并展示。

  4. pointer type

    reference type 传回一个左值令它代表p所指之物,而 pointer type则是“我们能够传回一个左值,代表所指之物的地址”。

    
    Item& operator*() const {return *ptr;}
    Item* operator->() const {return ptr;}
  5. iterator_category

    根据移动特性与施行操作,迭代器被分为五类:

    • input Iterator:这种迭代器所指的对象,不允许外界改变。只读(read only )

    • Output aerator:唯写(write only ).

    • Forward Iterator:允许“写入型”算法(例如replace())在此种迭代器所形成的区间上进行读写操作。

    • Bidirectional Iterator:可双向移动。某些算法需要逆向走访某个迭代器区间(例如逆向拷贝某范围内的元素),可以使用Bidirectional Iterators.

    • Random Access Iterator:前四种迭代器都只供应一部分指针算术能力(前三种支持。operator++,第四种再加上。operator--),第五种则涵盖所有指针算术能力,包括p+n,p-n,p[n],p1-pZ,pl<pZ。

      以上不同的迭代器类型性能上是有差异的。为了最佳性能,在一个函数调用时必须找到最优的解法。

      一种办法是放在运行期决定,比如:

      
      template <class InputIterator, class Distance>
        void advance(...){
          if(is_random_iterator()){
            random_advane();
          }else if(){
            
          }else{
            
          }
        }

      这种形式可以解决。但是更加好的办法是在编译时候就绑定。重载函数可以达成这个目的。又因为traits萃取的是类型。所以把其设计成类。

      
      struct input_iterator_tag {};
      struct output_iterator_tag {};
      struct forward_iterator_tag : public input_iterator_tag {};
      struct bidirectional_iterator_tag : public forward_iterator_tag {};
      struct random_access_iterator_tag : public bidirectional_iterator_tag {};
      //相应advance 例子
      template <class _InputIter, class _Distance>
      inline void __advance(_InputIter& __i, _Distance __n, input_iterator_tag) {
        while (__n--) ++__i;
      }
      
      template <class _BidirectionalIterator, class _Distance>
      inline void __advance(_BidirectionalIterator& __i, _Distance __n, 
                            bidirectional_iterator_tag) {
        if (__n >= 0)
          while (__n--) ++__i;
        else
          while (__n++) --__i;
      }
      
      template <class _RandomAccessIterator, class _Distance>
      inline void __advance(_RandomAccessIterator& __i, _Distance __n, 
                            random_access_iterator_tag) {
        __i += __n;
      }
      
      template <class _InputIterator, class _Distance>
      inline void advance(_InputIterator& __i, _Distance __n) {
        __advance(__i, __n, iterator_category(__i));//生成类 如下
      }
      iterator_category(const _Iter& __i) { return __iterator_category(__i); }
      
      template <class _Iter>
      inline typename iterator_traits<_Iter>::iterator_category
      __iterator_category(const _Iter&)
      {
        typedef typename iterator_traits<_Iter>::iterator_category _Category;
        return _Category();
      }

以上类型是iterator必须有的,否则和STL不兼容。

SGI STL的私房菜:__type_traits

Iterator_traits负责萃取迭代器的特性,_type_traits则负责萃取型别 ( type)的特性。此处我们所关注的型别特性是指:这个型别是否具备default的构造函数等。这样子,我们可以使用效率更高的memcpy(),memmove()等函数。

template <class _Tp>
struct __type_traits { 
   typedef __true_type     this_dummy_member_must_be_first;
                   /* Do not remove this member. It informs a compiler which
                      automatically specializes __type_traits that this
                      __type_traits template is special. It just makes sure that
                      things work if an implementation is using a template
                      called __type_traits for something unrelated. */

   /* The following restrictions should be observed for the sake of
      compilers which automatically produce type specific specializations 
      of this class:
          - You may reorder the members below if you wish
          - You may remove any of the members below if you wish
          - You must not rename members without making the corresponding
            name change in the compiler
          - Members you add will be treated like regular members unless
            you add the appropriate support in the compiler. */
 

   typedef __false_type    has_trivial_default_constructor;
   typedef __false_type    has_trivial_copy_constructor;
   typedef __false_type    has_trivial_assignment_operator;
   typedef __false_type    has_trivial_destructor;
   typedef __false_type    is_POD_type;
};
struct __true_type {
};

struct __false_type {
};
//全特化 #   define __STL_TEMPLATE_NULL template<>
__STL_TEMPLATE_NULL struct __type_traits<bool> {
   typedef __true_type    has_trivial_default_constructor;
   typedef __true_type    has_trivial_copy_constructor;
   typedef __true_type    has_trivial_assignment_operator;
   typedef __true_type    has_trivial_destructor;
   typedef __true_type    is_POD_type;
};

可以看到在泛化中,所有的属性都是false也就是说不会调用memcpy()等。而对于所有的C++的标量版本,都进行的全特化,以上仅仅为bool类型定义。

如果自己定义了类型,使用了默认的构造函数等,必须手动配置。

class A{};

template<> struct __type_traits<A> {
   typedef __true_type    has_trivial_default_constructor;
   typedef __true_type    has_trivial_copy_constructor;
   typedef __true_type    has_trivial_assignment_operator;
   typedef __true_type    has_trivial_destructor;
   typedef __true_type    is_POD_type;
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值