随笔-16  评论-7  文章-0  trackbacks-0

  1.迭代器

  迭代器提供了一种方法,使之能够依序巡访某个容器的各个元素,而无需暴露容器的内部。

    STL的中心思想是容器和算法分开,最后再用迭代器将两者完美的融合到一起。而迭代器是实现中最关键的部分。

  迭代器是一种行为类似指针的对象,因此它必不可少的就是对operator*和operator ->的重载。可以参考auto_ptr。

 

 

  2.traits技法(重点)

  在effective C++中,条款47介绍到了traits技术。它的思想是通过提取出来某些类型的特性,然后根据它的特性进行某些操作。

  一种方法是通过内嵌类来表示类型的特性,然后在进行操作的时候进行判断,然而这种方法却不能对内置类型使用。因为我们不可能向内置类型加入内嵌类以表示它的特性。

  另一种方法表示如下(在别人blog里面看到一个类似例子,自己写了一下):

 

#include <iostream>
using namespace std;

 

/* 下面两个空结构用来表示 */

struct hot{};
struct not_hot{};

 

/* traits用来提取模版参数T的特性 */

template 
<typename T>
struct traits_taste
{
 typedef typename T::taste taste;
}
;

 

/* 在类里面要定义自身的特性 */

class Hunan
{
public:
 typedef hot taste;
}
;
class Guangdong
{
public:
 typedef not_hot taste;
}
;

 


/* food函数用traits_taste将模板参数T的特征提取出来,交由food_aux根据特征执行 */


 

template 
<typename T>
void food_aux(T pro, not_hot)
{
 cout 
<< "Food in this province is not_hot!"<<endl;
}


 

int main()
{
 cout 
<< "Hunan:";
 food(Hunan());
 cout 
<< "Guangdong:";
 food(Guangdong());
 
return 0;
}


 

这种方式通过把类的traits信息放到类的外部,从而可以通过偏特化将内置类型的traits融入进去。

 

 在STL中,迭代器对应的traits类为iterator_traits

template <class I>
struct iterator_traits
{
 typedef typename I::iterator_category iterator_category;
 typedef typename I::value_type  value_type;
 typedef typename I::difference_type difference_type;
 typedef typename I::pointer  pointer;
 typedef typename I::reference  reference;
}

为了能够表示内置类型如int *, const int *等的特性,所以要设计相应的特化版本。下例是I*的特化。

 

 

template <class I>
struct iterator_traits<I*>
{
 typedef random_access_iterator_tag iterator_category;
 typedef typename I   value_type;
 typedef typename ptrdiff_t  difference_type;
 typedef typename I
*   pointer;
 typedef typename I
&   reference;
}
;

 

从此可以看出,在设计迭代器的时候一定要提供五个内嵌的相应型别。以利于traits萃取。STL提供了一个iterators class供人继承,可以保证其符合规范。

 

template <class Category,
   
class T,
   
class Distance=ptrdiff_t,
   
class Pointer=T*,
   
class Referance=T&>
struct iterator
{
 typedef Category   iterator_category;
 typedef typename T   value_type;
 typedef typename Distance  difference_type;
 typedef typename Pointer  pointer;
 typedef typename Referance  reference;
}
;

 

template 
<class Item>
struct ListIter: public std::iterator<std::forward_iterator_tag, Item>
{
 
}

 

3. __type_traits

这个是SGI内部的,不在STL标准内。用来萃取性别的特性。
先定义两个空结构体,用来标识true和false。

struct __true_type{};
struct __false_type{};

__type_traits内则定义一些typedefs,其值为__true_type或者__false_type

template<class type>
struct __type_traits
{
    typedef __false_type has_trivial_default_constructor;
    typedef __false_type has_trivial_copy_constructor;
    typedef __false_type has_trivial_assignment_constructor;
    typedef __false_type has_trivial_destructor;
    typedef __false_type is_POD_type;
}
;

 

posted on 2010-05-29 00:09 dead_horse 阅读(1173) 评论(1)  编辑 收藏 引用 所属分类: STL读书笔记

评论:
# re: 迭代器与traits技法(STL源码剖析)[未登录] 2012-07-20 21:20 | mm

楼主你好,我的站点,基本上转载了你的文章,觉得你的文章写的挺好的。刚好最近我这边在弄源码,百度一下 老是要进你的博客。

希望不要介意  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理