在阅读代码之前,要先了解一个新概念:Traits编程技法
template
struct MyIter
{
typedef T value_type //内嵌型别声明
T *ptr;
MyIter(T *p = 0) : ptr(p) { }
T& operator*() const {return *ptr}
// ....
};
template
typename I::value_type //func的返回类型
func(I ite)
{
return *ite;
}
MyIter ite(new int(8));
cout << func(ite) << endl; //8
template
struct iterator_traits {
typedef typename I::value_type value_type;
};
template
typename iterator_traits::value_type //函数返回类别
func(I ite)
{
return *ite;
}
tempalte
struct iterator_tratis { //偏特化,这个迭代器是原生态指针
typedef T value_type;
};
下面是stl_iterator.h源代码:
G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_iterator.h 完整列表
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
* Copyright (c) 1996,1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/* NOTE: This is an internal header file, included by other STL headers.
* You should not attempt to use it directly.
*/
#ifndef __SGI_STL_INTERNAL_ITERATOR_H
#define __SGI_STL_INTERNAL_ITERATOR_H
__STL_BEGIN_NAMESPACE
// 五种迭代器类型
/*
input_iterator_tag:只读(read only)
output_iterator_tag:只写(write only)
forward_iterator_tag:允许“写入型”算法在此种迭代器上操作
bidirectional_iterator_tag:允许双向操作
random_access_iterator_tag:涵盖原生态指针能力。
从定义上可以看出是继承关系 ,派生类型可以用于父类型,但是效率可能不佳
input_iterator_tag
|派生
forward_iterator_tag
|派生
bidirectional_iterator_tag
|派生
random_access_iterator_tag
*/
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 {};
//迭代器类型input_iterator
template struct input_iterator {
typedef input_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef T* pointer;
typedef T& reference;
};
//迭代器类型output_iterator
struct output_iterator {
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
};
//迭代器类型forward_iterator_tag
template struct forward_iterator {
typedef forward_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef T* pointer;
typedef T& reference;
};
//迭代器类型bidirectional_iterator
template struct bidirectional_iterator {
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef T* pointer;
typedef T& reference;
};
//迭代器类型random_access_iterator
template struct random_access_iterator {
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef T* pointer;
typedef T& reference;
};
#ifdef __STL_USE_NAMESPACES
//为了避免编码时挂一漏万,新设计的迭代器都继承下面这个 std::iterator
template
struct iterator {
typedef Category iterator_category;//迭代器类型
typedef T value_type;//值类型
typedef Distance difference_type;//迭代器之间的距离
typedef Pointer pointer;//指向对象原生态指针
typedef Reference reference;//对象的引用
};
#endif /* __STL_USE_NAMESPACES */
#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
// 以下是在支持partial specialization(偏特化) 的编译器上的实作方法
template
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;
};
// 针对原生态指针(native pointer)而设计的 traits 偏特化版。
template
struct iterator_traits {
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
};
//针对常量对象指针(const pointer)的traits偏特化版本
template
struct iterator_traits {
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef const T* pointer;
typedef const T& reference;
};
// 获取迭代器类型(category)
template
inline typename iterator_traits::iterator_category
iterator_category(const Iterator&) {
typedef typename iterator_traits::iterator_category category;
return category();
}
// 获取迭代器差值类型 distance type
template
inline typename iterator_traits::difference_type*
distance_type(const Iterator&) {
return static_cast::difference_type*>(0);
}
// 获取迭代器值类型 value type
template
inline typename iterator_traits::value_type*
value_type(const Iterator&) {
return static_cast::value_type*>(0);
}
#else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
//在不支持偏特化(partial specialization) 编译器上的作法
/*
5中迭代器类型x3个特性(iterator_tag、value_type、distance_type)
共15个函数
*/
template
inline input_iterator_tag
iterator_category(const input_iterator&) {
return input_iterator_tag();
}
inline output_iterator_tag iterator_category(const output_iterator&) {
return output_iterator_tag();
}
template
inline forward_iterator_tag
iterator_category(const forward_iterator&) {
return forward_iterator_tag();
}
template
inline bidirectional_iterator_tag
iterator_category(const bidirectional_iterator&) {
return bidirectional_iterator_tag();
}
template
inline random_access_iterator_tag
iterator_category(const random_access_iterator&) {
return random_access_iterator_tag();
}
template
inline random_access_iterator_tag iterator_category(const T*) {
return random_access_iterator_tag();
}
template
inline T* value_type(const input_iterator&) {
return (T*)(0);
}
template
inline T* value_type(const forward_iterator&) {
return (T*)(0);
}
template
inline T* value_type(const bidirectional_iterator&) {
return (T*)(0);
}
template
inline T* value_type(const random_access_iterator&) {
return (T*)(0);
}
template
inline T* value_type(const T*) { return (T*)(0); }
template
inline Distance* distance_type(const input_iterator&) {
return (Distance*)(0);
}
template
inline Distance* distance_type(const forward_iterator&) {
return (Distance*)(0);
}
template
inline Distance*
distance_type(const bidirectional_iterator&) {
return (Distance*)(0);
}
template
inline Distance*
distance_type(const random_access_iterator&) {
return (Distance*)(0);
}
template
inline ptrdiff_t* distance_type(const T*) { return (ptrdiff_t*)(0); }
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
// 以下是整組 distance 函式
/*
计算两个迭代器之间的距离。
用n来记录距离,n是引用reference,所以无函数返回值。
*/
template
inline void __distance(InputIterator first, InputIterator last, Distance& n,
input_iterator_tag) {
while (first != last) { ++first; ++n; }
}
template
inline void __distance(RandomAccessIterator first, RandomAccessIterator last,
Distance& n, random_access_iterator_tag) {
n += last - first;
}
template
inline void distance(InputIterator first, InputIterator last, Distance& n) {
__distance(first, last, n, iterator_category(first));
}
#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
// 以下是在支援 partial specialization 的编译器上的实作方法
//注意:有返回值。
template
inline iterator_traits::difference_type
__distance(InputIterator first, InputIterator last, input_iterator_tag) {
iterator_traits::difference_type n = 0;
while (first != last) {
++first; ++n;
}
return n;
}
template
inline iterator_traits::difference_type
__distance(RandomAccessIterator first, RandomAccessIterator last,
random_access_iterator_tag) {
return last - first;
}
template
inline iterator_traits::difference_type
distance(InputIterator first, InputIterator last) {
typedef typename iterator_traits::iterator_category category;
return __distance(first, last, category());
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
// 以下是整组 advance 函式
template
inline void __advance(InputIterator& i, Distance n, input_iterator_tag) {
while (n--) ++i;
}
#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma set woff 1183
#endif
template
inline void __advance(BidirectionalIterator& i, Distance n,
bidirectional_iterator_tag) {
if (n >= 0)
while (n--) ++i;
else
while (n++) --i;
}
#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma reset woff 1183
#endif
template
inline void __advance(RandomAccessIterator& i, Distance n,
random_access_iterator_tag) {
i += n;
}
template
inline void advance(InputIterator& i, Distance n) {
__advance(i, n, iterator_category(i));//根据迭代器类型来选择
}
/*
这是一个迭代器配接器(iterator adapter),用来将某个迭代器赋值,
insert-在容器尾端插进去,要求容器支持push_back
*/
template
class back_insert_iterator {
protected:
Container* container;
public:
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
//这个构造函数使 back_insert_iterator和x联接起来
explicit back_insert_iterator(Container& x) : container(&x) {}
back_insert_iterator&
operator=(const typename Container::value_type& value) {
container->push_back(value);//容器需要支持push_back操作
return *this;
}
//下面3个算子有定义,但无实际意义实现
back_insert_iterator& operator*() { return *this; }
back_insert_iterator& operator++() { return *this; }
back_insert_iterator& operator++(int) { return *this; }
};
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
//traits版本的 back_insert_iterator
template
inline output_iterator_tag
iterator_category(const back_insert_iterator&)
{
return output_iterator_tag();
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
// 这是一个辅助函数,帮助我们方便使用 back_insert_iterator。
template
inline back_insert_iterator back_inserter(Container& x) {
return back_insert_iterator(x);
}
//和上面的back_insert_iterator类似,只是在前面插入,要求容器支持 push_front
template
class front_insert_iterator {
protected:
Container* container;
public:
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
explicit front_insert_iterator(Container& x) : container(&x) {}
front_insert_iterator&
operator=(const typename Container::value_type& value) {
container->push_front(value);
return *this;
}
front_insert_iterator& operator*() { return *this; }
front_insert_iterator& operator++() { return *this; }
front_insert_iterator& operator++(int) { return *this; }
};
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
//traits版本的 back_insert_iterator
template
inline output_iterator_tag
iterator_category(const front_insert_iterator&)
{
return output_iterator_tag();
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
template
inline front_insert_iterator front_inserter(Container& x) {
return front_insert_iterator(x);
}
//和上面的back_insert_iterator类似,插入后迭代器会前进,要求容器支持 insert
template
class insert_iterator {
protected:
Container* container;
typename Container::iterator iter;
public:
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
//构造函数使 insert_iterator与容器x和迭代器i联接起来
insert_iterator(Container& x, typename Container::iterator i)
: container(&x), iter(i) {}
insert_iterator&
operator=(const typename Container::value_type& value) {
iter = container->insert(iter, value);
++iter; // 迭代器前进
return *this;
}
insert_iterator& operator*() { return *this; }
insert_iterator& operator++() { return *this; }
insert_iterator& operator++(int) { return *this; }
};
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
//traits版本的 insert_iterator
template
inline output_iterator_tag
iterator_category(const insert_iterator&)
{
return output_iterator_tag();
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
// 辅助函数,帮助使用insert_iterator。
template
inline insert_iterator inserter(Container& x, Iterator i) {
typedef typename Container::iterator iter;
return insert_iterator(x, iter(i));
}
//迭代器配置器,将迭代器逆反方向前进,是前进为后退,后退为前进
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template
#else
template
#endif
class reverse_bidirectional_iterator {
typedef reverse_bidirectional_iterator self;
protected:
BidirectionalIterator current;
public:
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef T* pointer;
typedef Reference reference;
reverse_bidirectional_iterator() {}
explicit reverse_bidirectional_iterator(BidirectionalIterator x)
: current(x) {}
BidirectionalIterator base() const { return current; }
Reference operator*() const {
BidirectionalIterator tmp = current;
return *--tmp;
}
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
// ++ 为 -- ,前进变后退
self& operator++() {
--current;
return *this;
}
self operator++(int) {
self tmp = *this;
--current;
return tmp;
}
// -- 为 ++,后退 变前进
self& operator--() {
++current;
return *this;
}
self operator--(int) {
self tmp = *this;
++current;
return tmp;
}
};
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
//traits版本
template
inline bidirectional_iterator_tag
iterator_category(const reverse_bidirectional_iterator&) {
return bidirectional_iterator_tag();
}
template
inline T*
value_type(const reverse_bidirectional_iterator&) {
return (T*) 0;
}
template
inline Distance*
distance_type(const reverse_bidirectional_iterator&) {
return (Distance*) 0;
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
template
inline bool operator==(
const reverse_bidirectional_iterator& x,
const reverse_bidirectional_iterator& y) {
return x.base() == y.base();
}
#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
/*
以下是C++标准定义的reverse iterator,定义于C++标准草稿中。它依赖
iterator_traits template,并依此依赖partial specialization。
先前的那个 reverse_bidirectional_iterator不再是标准草稿的一部分,
但仍然保留以便回 溯相容。
*/
//这是迭代器配置器,用来将迭代器将逆反前进方向,前进变后退,后退变前进。
template
class reverse_iterator
{
protected:
Iterator current;
public:
typedef typename iterator_traits::iterator_category
iterator_category;
typedef typename iterator_traits::value_type
value_type;
typedef typename iterator_traits::difference_type
difference_type;
typedef typename iterator_traits::pointer
pointer;
typedef typename iterator_traits::reference
reference;
typedef Iterator iterator_type;
typedef reverse_iterator self;
public:
reverse_iterator() {}
explicit reverse_iterator(iterator_type x) : current(x) {}
reverse_iterator(const self& x) : current(x.current) {}
#ifdef __STL_MEMBER_TEMPLATES
template
reverse_iterator(const reverse_iterator& x) : current(x.current) {}
#endif /* __STL_MEMBER_TEMPLATES */
iterator_type base() const { return current; }
reference operator*() const {
Iterator tmp = current;
return *--tmp; // 关键设计
}
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
// ++ 变 --
self& operator++() {
--current;
return *this;
}
self operator++(int) {
self tmp = *this;
--current;
return tmp;
}
// -- 变 ++
self& operator--() {
++current;
return *this;
}
self operator--(int) {
self tmp = *this;
++current;
return tmp;
}
// 前进和后退方向完全逆转
self operator+(difference_type n) const {
return self(current - n);
}
self& operator+=(difference_type n) {
current -= n;
return *this;
}
self operator-(difference_type n) const {
return self(current + n);
}
self& operator-=(difference_type n) {
current += n;
return *this;
}
//下面第一个*和唯一的 + 都会唤起本类别的 operator* 和 operator+
//第二个则不会 。判断法则:完全看待处理的类别是什么而定
reference operator[](difference_type n) const { return *(*this + n); }
};
template
inline bool operator==(const reverse_iterator& x,
const reverse_iterator& y) {
return x.base() == y.base();
}
template
inline bool operator<(const reverse_iterator& x,
const reverse_iterator& y) {
return y.base() < x.base();
}
template
inline typename reverse_iterator::difference_type
operator-(const reverse_iterator& x,
const reverse_iterator& y) {
return y.base() - x.base();
}
template
inline reverse_iterator
operator+(reverse_iterator::difference_type n,
const reverse_iterator& x) {
return reverse_iterator(x.base() - n);
}
#else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
//不支持偏特化时, reverse_iterator代码
//HP STL
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template
#else
template
#endif
class reverse_iterator {
typedef reverse_iterator
self;
protected:
RandomAccessIterator current;
public:
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef T* pointer;
typedef Reference reference;
reverse_iterator() {}
explicit reverse_iterator(RandomAccessIterator x) : current(x) {}
RandomAccessIterator base() const { return current; }
Reference operator*() const { return *(current - 1); }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
self& operator++() {
--current;
return *this;
}
self operator++(int) {
self tmp = *this;
--current;
return tmp;
}
self& operator--() {
++current;
return *this;
}
self operator--(int) {
self tmp = *this;
++current;
return tmp;
}
self operator+(Distance n) const {
return self(current - n);
}
self& operator+=(Distance n) {
current -= n;
return *this;
}
self operator-(Distance n) const {
return self(current + n);
}
self& operator-=(Distance n) {
current += n;
return *this;
}
Reference operator[](Distance n) const { return *(*this + n); }
};
template
inline random_access_iterator_tag
iterator_category(const reverse_iterator&) {
return random_access_iterator_tag();
}
template
inline T* value_type(const reverse_iterator&) {
return (T*) 0;
}
template
inline Distance* distance_type(const reverse_iterator&) {
return (Distance*) 0;
}
template
inline bool operator==(const reverse_iterator& x,
const reverse_iterator& y) {
return x.base() == y.base();
}
template
inline bool operator<(const reverse_iterator& x,
const reverse_iterator& y) {
return y.base() < x.base();
}
template
inline Distance operator-(const reverse_iterator& x,
const reverse_iterator& y) {
return y.base() - x.base();
}
template
inline reverse_iterator
operator+(Dist n, const reverse_iterator& x) {
return reverse_iterator(x.base() - n);
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
/*
这是一个input iterator,能够为[来自某一 basic istream]的物件执行格式化输入动作。
注意:此版本为含有HP的规格,为符合标准介面。
一般使用 input iterators时都只使用第一个template参数,此时以下仍 适用。
注:sGI STL 3.3已经做出符合标准界面的 istream_iterator。作法和本版本大同小异。
本版本可能性较高。
*/
template
class istream_iterator {
friend bool
operator== __STL_NULL_TMPL_ARGS (const istream_iterator& x,
const istream_iterator& y);
// 以上语法很奇特,请参考C++ Primer p834: bound friend function template
// 在 中,__STL_NULL_TMPL_ARGS 被定义为 <>
protected:
istream* stream;
T value;
bool end_marker;
void read() {
end_marker = (*stream) ? true : false;
if (end_marker) *stream >> value;
//以上,输入之后,stream的状态可能改变,所以下面在判断一次一决定end_marker
//当读到EOF或读到型别不符的资料,stream即处于false状态。
end_marker = (*stream) ? true : false;
}
public:
typedef input_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef const T* pointer;
typedef const T& reference;
// 以上,因身为input iterator,所以拟用 const 比较保险
// 下面這些构造函数 使 istream_iterator 和某个 istream object 联接起來。
istream_iterator() : stream(&cin), end_marker(false) {}
istream_iterator(istream& s) : stream(&s) { read(); }
// 以上两行的用法:
// istream_iterator eos; 造成 end_marker 为 false。
// istream_iterator initer(cin); 引发 read()。程式至此等待输入。
// 因此,下面这两行客端程式:
// istream_iterator initer(cin); (A)
// cout << "please input..." << endl; (B)
// 会停留在 (A) 等待一个输入,然后才执行 (B) 出现提示信息。这是不合理现象。
// 规避之道:永远在最必要的时候,才定义一个 istream_iterator。
reference operator*() const { return value; }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
//迭代器的前一个位置,就代表要读取 一笔资料
istream_iterator& operator++() {
read();
return *this;
}
istream_iterator operator++(int) {
istream_iterator tmp = *this;
read();
return tmp;
}
};
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
template
inline input_iterator_tag
iterator_category(const istream_iterator&) {
return input_iterator_tag();
}
template
inline T* value_type(const istream_iterator&) { return (T*) 0; }
template
inline Distance* distance_type(const istream_iterator&) {
return (Distance*) 0;
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
template
inline bool operator==(const istream_iterator& x,
const istream_iterator& y) {
return x.stream == y.stream && x.end_marker == y.end_marker ||
x.end_marker == false && y.end_marker == false;
}
//和前面读入类似,这个是输出
template
class ostream_iterator {
protected:
ostream* stream;
const char* string; // 每次输出后的间隔符号。
// 以上注意,可以將变数命名為 string 吗?可以,但稍后如需使用
// C++ library string,得为 std::string.
public:
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
ostream_iterator(ostream& s) : stream(&s), string(0) {}
ostream_iterator(ostream& s, const char* c) : stream(&s), string(c) {}
// 以上 构造函数 的用法:
// ostream_iterator outiter(cout, ' '); 输出至 cout,每次间隔一个空格
// 对迭代器做赋值(assign)动作,就代表要输出一笔资料
ostream_iterator& operator=(const T& value) {
*stream << value; // 先输出数值
if (string) *stream << string; // 如果状态无误,再输出间隔符号
return *this;
}
ostream_iterator& operator*() { return *this; }
ostream_iterator& operator++() { return *this; }
ostream_iterator& operator++(int) { return *this; }
};
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
template
inline output_iterator_tag
iterator_category(const ostream_iterator&) {
return output_iterator_tag();
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
__STL_END_NAMESPACE
#endif /* __SGI_STL_INTERNAL_ITERATOR_H */
// Local Variables:
// mode:C++
// End: