stl_iterator.h
stl_iterator.h
// Filename: stl_iterator.h // Comment By: 凝霜
// E-mail: mdl2009@vip.qq.com
// Blog: http://blog.csdn.net/mdl13412 /*
*
* 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 ////////////////////////////////////////////////////////////////////////////////
// STL迭代器定义
////////////////////////////////////////////////////////////////////////////////
// STL中有五种迭代器类型
// Input Iterator read only
// Output Iterator write only
// Forward Iterator 允许"写入型"算法在其指向区间进行操作
// Bidirectional Iterator 提供双向访问能力
// Random Access Iterator 支持原生指针具有的全部能力
////////////////////////////////////////////////////////////////////////////////
// 类型从属关系, 子类适用于接受父类类型的算法, 但是效率可能不佳
//
// Input Iterator
// ↑
// Forward Iterator
// ↑
// Bidirectional Iterator
// ↑
// Random Access Iterator
//////////////////////////////////////////////////////////////////////////////// // 用于标记迭代器类型
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 {}; template <class T, class Distance> struct input_iterator
{
typedef input_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef T* pointer;
typedef T& reference;
}; struct output_iterator
{
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
}; template <class T, class Distance> struct forward_iterator
{
typedef forward_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef T* pointer;
typedef T& reference;
}; template <class T, class Distance> struct bidirectional_iterator
{
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef T* pointer;
typedef T& reference;
}; template <class T, class Distance> 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
template <class Category, class T, class Distance = ptrdiff_t,
class Pointer = T*, class Reference = T&>
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 ////////////////////////////////////////////////////////////////////////////////
// iterator_traits定义
//////////////////////////////////////////////////////////////////////////////// // 用于traits出迭代其所指对象的型别
template <class Iterator>
struct iterator_traits
{
// 迭代器类型, STL提供五种迭代器
typedef typename Iterator::iterator_category iterator_category; // 迭代器所指对象的型别
// 如果想与STL算法兼容, 那么在类内需要提供value_type定义
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 T* pointer;
typedef T& reference;
}; // 针对指向常对象的指针提供特化
template <class T>
struct iterator_traits<const T*>
{
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef const T* pointer;
typedef const T& reference;
}; ////////////////////////////////////////////////////////////////////////////////
// iterator_traits支持函数
////////////////////////////////////////////////////////////////////////////////
// iterator_category(const Iterator&) 返回迭代器类别
// distance_type(const Iterator&) 返回表示迭代器距离的类型
// value_type(const Iterator&) 返回迭代器所指对象的类型
//////////////////////////////////////////////////////////////////////////////// template <class Iterator>
inline typename iterator_traits<Iterator>::iterator_category
iterator_category(const Iterator&)
{
typedef typename iterator_traits<Iterator>::iterator_category category;
return category();
} template <class Iterator>
inline typename iterator_traits<Iterator>::difference_type*
distance_type(const Iterator&)
{
return static_cast<typename iterator_traits<Iterator>::difference_type*>();
} template <class Iterator>
inline typename iterator_traits<Iterator>::value_type*
value_type(const Iterator&)
{
return static_cast<typename iterator_traits<Iterator>::value_type*>();
} #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */ // 编译器不支持partial specialization of class templates(模板类偏特化)
// 需要对所有迭代器类型都提供定义 template <class T, class Distance>
inline input_iterator_tag
iterator_category(const input_iterator<T, Distance>&)
{
return input_iterator_tag();
} inline output_iterator_tag iterator_category(const output_iterator&)
{
return output_iterator_tag();
} template <class T, class Distance>
inline forward_iterator_tag
iterator_category(const forward_iterator<T, Distance>&)
{
return forward_iterator_tag();
} template <class T, class Distance>
inline bidirectional_iterator_tag
iterator_category(const bidirectional_iterator<T, Distance>&)
{
return bidirectional_iterator_tag();
} template <class T, class Distance>
inline random_access_iterator_tag
iterator_category(const random_access_iterator<T, Distance>&)
{
return random_access_iterator_tag();
} template <class T>
inline random_access_iterator_tag iterator_category(const T*)
{
return random_access_iterator_tag();
} template <class T, class Distance>
inline T* value_type(const input_iterator<T, Distance>&)
{
return (T*)();
} template <class T, class Distance>
inline T* value_type(const forward_iterator<T, Distance>&)
{
return (T*)();
} template <class T, class Distance>
inline T* value_type(const bidirectional_iterator<T, Distance>&)
{
return (T*)();
} template <class T, class Distance>
inline T* value_type(const random_access_iterator<T, Distance>&)
{
return (T*)();
} template <class T>
inline T* value_type(const T*) { return (T*)(); } template <class T, class Distance>
inline Distance* distance_type(const input_iterator<T, Distance>&)
{
return (Distance*)();
} template <class T, class Distance>
inline Distance* distance_type(const forward_iterator<T, Distance>&)
{
return (Distance*)();
} template <class T, class Distance>
inline Distance*
distance_type(const bidirectional_iterator<T, Distance>&)
{
return (Distance*)();
} template <class T, class Distance>
inline Distance*
distance_type(const random_access_iterator<T, Distance>&)
{
return (Distance*)();
} template <class T>
inline ptrdiff_t* distance_type(const T*) { return (ptrdiff_t*)(); } #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ ////////////////////////////////////////////////////////////////////////////////
// template <class InputIterator, class Distance>
// inline void distance(InputIterator first, InputIterator last, Distance& n)
////////////////////////////////////////////////////////////////////////////////
// distance
// |
// |---------------- 判断迭代器类型
// Input Iterator ↓ Random Access Iterator
// -------------------------------------------
// | |
// | |
// ↓ |
// __distance(..., input_iterator_tag) |
// while (first != last) { ++first; ++n; } |
// ↓
// __distance(..., random_access_iterator_tag)
// n += last - first;
//////////////////////////////////////////////////////////////////////////////// template <class InputIterator, class Distance>
inline void __distance(InputIterator first, InputIterator last, Distance& n,
input_iterator_tag)
{
while (first != last) { ++first; ++n; }
} template <class RandomAccessIterator, class Distance>
inline void __distance(RandomAccessIterator first, RandomAccessIterator last,
Distance& n, random_access_iterator_tag)
{
n += last - first;
} template <class InputIterator, class Distance>
inline void distance(InputIterator first, InputIterator last, Distance& n)
{
__distance(first, last, n, iterator_category(first));
} #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION ////////////////////////////////////////////////////////////////////////////////
// template <class InputIterator>
// inline iterator_traits<InputIterator>::difference_type
// distance(InputIterator first, InputIterator last)
////////////////////////////////////////////////////////////////////////////////
// distance
// |
// |---------------- 判断迭代器类型
// Input Iterator ↓ Random Access Iterator
// -------------------------------------------
// | |
// | |
// ↓ |
// __distance(..., input_iterator_tag) |
// while (first != last) { |
// ++first; ++n; |
// } |
// return n; |
// ↓
// __distance(..., random_access_iterator_tag)
// return last - first;
//////////////////////////////////////////////////////////////////////////////// template <class InputIterator>
inline iterator_traits<InputIterator>::difference_type
__distance(InputIterator first, InputIterator last, input_iterator_tag)
{
iterator_traits<InputIterator>::difference_type n = ;
while (first != last) {
++first; ++n;
}
return n;
} template <class RandomAccessIterator>
inline iterator_traits<RandomAccessIterator>::difference_type
__distance(RandomAccessIterator first, RandomAccessIterator last,
random_access_iterator_tag)
{
return last - first;
} template <class InputIterator>
inline iterator_traits<InputIterator>::difference_type
distance(InputIterator first, InputIterator last)
{
typedef typename iterator_traits<InputIterator>::iterator_category category;
return __distance(first, last, category());
} #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ ////////////////////////////////////////////////////////////////////////////////
// advance()实现部分
////////////////////////////////////////////////////////////////////////////////
// advance
// |
// |---------------- 判断迭代器类型
// Input Iterator ↓
// ---------------------------------------------------------------------
// | Random Access Iterator | Bidirectional Iterator |
// | | |
// ↓ | |
// __advance(..., input_iterator_tag) | |
// while (n--) ++i; | |
// | |
// ↓ |
// __advance(..., random_access_iterator_tag) |
// i += n; |
// |
// ↓
// __advance(..., bidirectional_iterator_tag)
// if (n >= 0)
// while (n--) ++i;
// else
// while (n++) --i;
//////////////////////////////////////////////////////////////////////////////// template <class InputIterator, class Distance>
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 <class BidirectionalIterator, class Distance>
inline void __advance(BidirectionalIterator& i, Distance n,
bidirectional_iterator_tag)
{
if (n >= )
while (n--) ++i;
else
while (n++) --i;
} #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma reset woff 1183
#endif 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));
} ////////////////////////////////////////////////////////////////////////////////
// back_insert_iterator实现部分
//////////////////////////////////////////////////////////////////////////////// template <class Container>
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; explicit back_insert_iterator(Container& x) : container(&x) {} // 只有提供了push_back()操作的容器才能使用back_insert_iterator
back_insert_iterator<Container>&
operator=(const typename Container::value_type& value)
{
container->push_back(value);
return *this;
} back_insert_iterator<Container>& operator*() { return *this; }
back_insert_iterator<Container>& operator++() { return *this; }
back_insert_iterator<Container>& operator++(int) { return *this; }
}; #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION // 用于traits出back_insert_iterator的迭代器类别
template <class Container>
inline output_iterator_tag
iterator_category(const back_insert_iterator<Container>&)
{
return output_iterator_tag();
} #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ template <class Container>
inline back_insert_iterator<Container> back_inserter(Container& x)
{
return back_insert_iterator<Container>(x);
} ////////////////////////////////////////////////////////////////////////////////
// front_insert_iterator实现部分
//////////////////////////////////////////////////////////////////////////////// template <class Container>
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) {} // 只有提供了push_front()操作的容器才能使用front_insert_iterator
front_insert_iterator<Container>&
operator=(const typename Container::value_type& value)
{
container->push_front(value);
return *this;
}
front_insert_iterator<Container>& operator*() { return *this; }
front_insert_iterator<Container>& operator++() { return *this; }
front_insert_iterator<Container>& operator++(int) { return *this; }
}; #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION template <class Container>
inline output_iterator_tag
iterator_category(const front_insert_iterator<Container>&)
{
return output_iterator_tag();
} #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ template <class Container>
inline front_insert_iterator<Container> front_inserter(Container& x)
{
return front_insert_iterator<Container>(x);
} ////////////////////////////////////////////////////////////////////////////////
// insert_iterator实现部分
//////////////////////////////////////////////////////////////////////////////// template <class Container>
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(Container& x, typename Container::iterator i)
: container(&x), iter(i) {} // 只有提供了insert操作的容器才能使用insert_iterator
insert_iterator<Container>&
operator=(const typename Container::value_type& value)
{
iter = container->insert(iter, value);
++iter;
return *this;
}
insert_iterator<Container>& operator*() { return *this; }
insert_iterator<Container>& operator++() { return *this; }
insert_iterator<Container>& operator++(int) { return *this; }
}; #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION template <class Container>
inline output_iterator_tag
iterator_category(const insert_iterator<Container>&)
{
return output_iterator_tag();
} #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ template <class Container, class Iterator>
inline insert_iterator<Container> inserter(Container& x, Iterator i)
{
typedef typename Container::iterator iter;
return insert_iterator<Container>(x, iter(i));
} ////////////////////////////////////////////////////////////////////////////////
// reverse_bidirectional_iterator实现部分
//////////////////////////////////////////////////////////////////////////////// // reverse_bidirectional_iterator使用的是BidirectionalIterator
// 所以要对operator *(), ++(), ++(int)都提供处理
// 同时因为是反向迭代器, 所以重载运算符的操作要特殊处理
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class BidirectionalIterator, class T, class Reference = T&,
class Distance = ptrdiff_t>
#else
template <class BidirectionalIterator, class T, class Reference,
class Distance>
#endif
class reverse_bidirectional_iterator
{
typedef reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
Distance> 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 template <class BidirectionalIterator, class T, class Reference,
class Distance>
inline bidirectional_iterator_tag
iterator_category(const reverse_bidirectional_iterator<BidirectionalIterator,
T,
Reference, Distance>&)
{
return bidirectional_iterator_tag();
} template <class BidirectionalIterator, class T, class Reference,
class Distance>
inline T*
value_type(const reverse_bidirectional_iterator<BidirectionalIterator, T,
Reference, Distance>&)
{
return (T*) ;
} template <class BidirectionalIterator, class T, class Reference,
class Distance>
inline Distance*
distance_type(const reverse_bidirectional_iterator<BidirectionalIterator, T,
Reference, Distance>&)
{
return (Distance*) ;
} #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ template <class BidirectionalIterator, class T, class Reference,
class Distance>
inline bool operator==(
const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
Distance>& x,
const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
Distance>& y)
{
return x.base() == y.base();
} #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION ////////////////////////////////////////////////////////////////////////////////
// reverse_iterator实现部分
//////////////////////////////////////////////////////////////////////////////// // This is the new version of reverse_iterator, as defined in the
// draft C++ standard. It relies on the iterator_traits template,
// which in turn relies on partial specialization. The class
// reverse_bidirectional_iterator is no longer part of the draft
// standard, but it is retained for backward compatibility. template <class Iterator>
class reverse_iterator
{
protected:
Iterator current;
public:
typedef typename iterator_traits<Iterator>::iterator_category
iterator_category;
typedef typename iterator_traits<Iterator>::value_type
value_type;
typedef typename iterator_traits<Iterator>::difference_type
difference_type;
typedef typename iterator_traits<Iterator>::pointer
pointer;
typedef typename iterator_traits<Iterator>::reference
reference; typedef Iterator iterator_type;
typedef reverse_iterator<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 <class Iter>
reverse_iterator(const reverse_iterator<Iter>& 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;
}
reference operator[](difference_type n) const { return *(*this + n); }
}; template <class Iterator>
inline bool operator==(const reverse_iterator<Iterator>& x,
const reverse_iterator<Iterator>& y)
{
return x.base() == y.base();
} template <class Iterator>
inline bool operator<(const reverse_iterator<Iterator>& x,
const reverse_iterator<Iterator>& y)
{
return y.base() < x.base();
} template <class Iterator>
inline typename reverse_iterator<Iterator>::difference_type
operator-(const reverse_iterator<Iterator>& x,
const reverse_iterator<Iterator>& y)
{
return y.base() - x.base();
} template <class Iterator>
inline reverse_iterator<Iterator>
operator+(reverse_iterator<Iterator>::difference_type n,
const reverse_iterator<Iterator>& x)
{
return reverse_iterator<Iterator>(x.base() - n);
} #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */ // 如果不支持partial specialization of class templates(模板类偏特化)
// 则使用HP STL的实现 // This is the old version of reverse_iterator, as found in the original
// HP STL. It does not use partial specialization. #ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class RandomAccessIterator, class T, class Reference = T&,
class Distance = ptrdiff_t>
#else
template <class RandomAccessIterator, class T, class Reference,
class Distance>
#endif
class reverse_iterator
{
typedef reverse_iterator<RandomAccessIterator, T, Reference, Distance>
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 - ); }
#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 <class RandomAccessIterator, class T, class Reference, class Distance>
inline random_access_iterator_tag
iterator_category(const reverse_iterator<RandomAccessIterator, T,
Reference, Distance>&)
{
return random_access_iterator_tag();
} template <class RandomAccessIterator, class T, class Reference, class Distance>
inline T* value_type(const reverse_iterator<RandomAccessIterator, T,
Reference, Distance>&)
{
return (T*) ;
} template <class RandomAccessIterator, class T, class Reference, class Distance>
inline Distance* distance_type(const reverse_iterator<RandomAccessIterator, T,
Reference, Distance>&)
{
return (Distance*) ;
} template <class RandomAccessIterator, class T, class Reference, class Distance>
inline bool operator==(const reverse_iterator<RandomAccessIterator, T,
Reference, Distance>& x,
const reverse_iterator<RandomAccessIterator, T,
Reference, Distance>& y)
{
return x.base() == y.base();
} template <class RandomAccessIterator, class T, class Reference, class Distance>
inline bool operator<(const reverse_iterator<RandomAccessIterator, T,
Reference, Distance>& x,
const reverse_iterator<RandomAccessIterator, T,
Reference, Distance>& y)
{
return y.base() < x.base();
} template <class RandomAccessIterator, class T, class Reference, class Distance>
inline Distance operator-(const reverse_iterator<RandomAccessIterator, T,
Reference, Distance>& x,
const reverse_iterator<RandomAccessIterator, T,
Reference, Distance>& y)
{
return y.base() - x.base();
} template <class RandomAccessIter, class T, class Ref, class Dist>
inline reverse_iterator<RandomAccessIter, T, Ref, Dist>
operator+(Dist n, const reverse_iterator<RandomAccessIter, T, Ref, Dist>& x)
{
return reverse_iterator<RandomAccessIter, T, Ref, Dist>(x.base() - n);
} #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ ////////////////////////////////////////////////////////////////////////////////
// istream_iterator实现部分
//////////////////////////////////////////////////////////////////////////////// template <class T, class Distance = ptrdiff_t>
class istream_iterator
{
friend bool
operator== __STL_NULL_TMPL_ARGS (const istream_iterator<T, Distance>& x,
const istream_iterator<T, Distance>& y);
protected:
istream* stream;
T value;
bool end_marker;
void read() {
end_marker = (*stream) ? true : false;
if (end_marker) *stream >> value;
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; istream_iterator() : stream(&cin), end_marker(false) {}
istream_iterator(istream& s) : stream(&s) { read(); }
reference operator*() const { return value; }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
istream_iterator<T, Distance>& operator++() {
read();
return *this;
}
istream_iterator<T, Distance> operator++(int) {
istream_iterator<T, Distance> tmp = *this;
read();
return tmp;
}
}; #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION template <class T, class Distance>
inline input_iterator_tag
iterator_category(const istream_iterator<T, Distance>&)
{
return input_iterator_tag();
} template <class T, class Distance>
inline T* value_type(const istream_iterator<T, Distance>&) { return (T*) ; } template <class T, class Distance>
inline Distance* distance_type(const istream_iterator<T, Distance>&)
{
return (Distance*) ;
} #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ template <class T, class Distance>
inline bool operator==(const istream_iterator<T, Distance>& x,
const istream_iterator<T, Distance>& y)
{
return x.stream == y.stream && x.end_marker == y.end_marker ||
x.end_marker == false && y.end_marker == false;
} ////////////////////////////////////////////////////////////////////////////////
// ostream_iterator实现部分
//////////////////////////////////////////////////////////////////////////////// template <class T>
class ostream_iterator
{
protected:
ostream* stream;
const char* 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() {}
ostream_iterator(ostream& s, const char* c) : stream(&s), string(c) {}
ostream_iterator<T>& operator=(const T& value) {
*stream << value;
if (string) *stream << string;
return *this;
}
ostream_iterator<T>& operator*() { return *this; }
ostream_iterator<T>& operator++() { return *this; }
ostream_iterator<T>& operator++(int) { return *this; }
}; #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION template <class T>
inline output_iterator_tag
iterator_category(const ostream_iterator<T>&)
{
return output_iterator_tag();
} #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ __STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_ITERATOR_H */ // Local Variables:
// mode:C++
// End:
stl_iterator.h的更多相关文章
- 《STL源代码分析》---stl_list.h读书笔记
STL在列表list它是一种经常使用的容器.list不连续双向链表在内存,而且是环形. 理解列表如何操作的详细信息,然后.阅读STL名单上的代码是最好的方法. G++ 2.91.57.cygnus\c ...
- stl_algobase.h
stl_algobase.h // Filename: stl_algobase.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ...
- stl_list.h
stl_list.h // Filename: stl_list.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http://b ...
- stl_slist.h
stl_slist.h // Filename: stl_slist.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http:/ ...
- STL源代码剖析——基本算法stl_algobase.h
前言 在STL中.算法是常常被使用的,算法在整个STL中起到很关键的数据.本节介绍的是一些基本算法,包括equal.fill.fill_n,iter_swap.lexicographical_comp ...
- Linux覆盖率一点研究:获取覆盖率数据
首先,当然哥不介意你鄙视我在网上找的资料研究! 白盒覆盖率是啥东东这个问题大家自己查百度啦!我也不太懂,就知道它不是个东西,就这样开始吧(MT一般是先摸四蹄呢还是先黑金币呢?这是个问题)! 首先:l ...
- 【转】三十分钟掌握STL
转自http://net.pku.edu.cn/~yhf/UsingSTL.htm 三十分钟掌握STL 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以 ...
- 三十分钟掌握STL
这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把它翻译出来.我没有对翻译出来的内容校验过.如果你没法在三十分钟内觉得有所收获,那么赶紧扔了 ...
- 【转】三十分钟学会STL算法
转载自: http://net.pku.edu.cn/~yhf/UsingSTL.htm 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把 ...
随机推荐
- java Collection-Map 之 TreeMap
TreeMap 内部定义了一个类 static final class Entry<K,V> implements Map.Entry<K,V>,(自平衡红黑二叉树)作为数据 ...
- Unity批量生成Prefab
在项目中有时会遇到批量生成Prefab的需求.于是写了一个编辑器,用来实现此功能. 在Hierarchy面板中选中多个GameObject,点击生成Prefab即可. 如果所选物体中包含自定义Mesh ...
- Lumen开发:lumen源码解读之初始化(5)——注册(register)与启动(boot)
版权声明:本文为博主原创文章,未经博主允许不得转载. register()是在服务容器注册服务, bootstrap/app.php /** * 注册外部服务 */ $app->register ...
- Python 字符串拼接、格式化输出、深浅复制
拼接:"+"号(同类型可拼接) >>>li = [1,2] >>>li + li [1,2,1,2] >>>li*2 [1,2 ...
- 九度OJ 1208:10进制 VS 2进制 (进制转换)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2040 解决:612 题目描述: 对于一个十进制数A,将A转换为二进制数,然后按位逆序排列,再转换为十进制数B,我们乘B为A的二进制逆序数. ...
- python脚本分析nginx访问日志
日志格式如下: 223.74.135.248 [11/May/2017:11:19:47 +0800] "POST /login/getValidateCode HTTP/1.1" ...
- Image Recognition
https://www.tensorflow.org/tutorials/image_recognition
- Python菜鸟之路:Python基础-类(1)——概念
什么是类? 在python中,把具有相同属性和方法的对象归为一个类(class).类是对象的模板或蓝图,类是对象的抽象化,对象是类的实例化.类不代表具体的事物,而对象表示具体的事物. 类的创建 cla ...
- Linux:分区
Linux:分区 分区表 磁盘分区表主要有两种格式,一种是限制较多的 MBR 分区表,一种是较新且限制较少的 GPT 分区表. MBR MBR 中,第一个扇区最重要,里面有主要开机记录(Master ...
- 实用篇:说说我在JavaScript项目中使用的工具类
在JavaScript的开发中,我们都会写一些工具类来帮我们简化一些业务操作的逻辑,一下就貼几个我在项目开发过程中常用的工具类.表达能力有限,各位看官还是看源码吧. 一.日期处理工具类. /** * ...