适配器
适配器在STL组件的中扮演转接器作用,事实上是一种设计模式,将一个class的接口转接为另一个class接口,使原本接口不能兼容的classes合作使用。
改变仿函数接口称为function adapter,改变容器接口称为container adapter,改变迭代器接口称为iterator adapter。
STL提供的queue和stack就是通过改变deque接口的适配器。前面文章已作介绍。
迭代器适配器
insert iterators,reverse iterators,iostream iterators便是迭代器适配器。
insert iterators可以将一般迭代器的赋值转换为插入操作,提供首尾及任意位置插入操作。
reverse iterators
reverse iterators可以将一般迭代器反向逆转,operator++变为后退操作,operator- -变为前进操作。
在双向序列vector,list,deque中的rbegin()和rend()定义,
typedef std::reverse_iterator<iterator> reverse_iterator; //反向迭代器
reverse_iterator rbegin()
{ return reverse_iterator(end()); }
reverse_iterator rend()
{ return reverse_iterator(begin()); }
stack, queue, priority_queue不提供begin()和end(),也就没有rbegin()和rend(),所以单向序列及stack, queue, priority_queue不可使用reverse_iterator。
当迭代器被反转时,虽然地址不变,但其指向元素发生改变,相对正向迭代器位置-1。
insert iterators
insert iterators内部维护一个用户指定的容器,容器有自己的迭代器,当对insert iterators做赋值(assign)操作时,在insert iterators中被转为对该容器的迭代器做插入操作。
在insert iterators的operator=操作符中调用底层容器的push_front()或push_back()或insert()操作函数。关闭operator++、operator++(int)、operator- -、operator- -(int)、operator* 、operator->功能,迭代器类型定义为output_iterator_tag。
back_insert_iterator的赋值操作实际执行底层容器的尾端插入。
front_insert_iterator的赋值操作实际执行底层容器的头部插入。
insert_iterator的赋值操作实际执行底层容器的插入操作。
iostream iterators
iostream iterators可以将迭代器绑定到iostream对象上,绑定到istream对象上的迭代器称istream_iterator拥有输入功能,迭代器在内部维护一个istream成员,迭代器operator++操作实际执行内部流成员的operator>>操作,不具备operator- -。
istream_iterator在istream_iterator(istream_type& __s)构造函数时初始化内置流对象停在read函数等待输入,所以必须在绝对必要的时刻才以此构造函数定义输入流迭代器。
绑定到ostream对象上的迭代器称ostream_iterator拥有输出功能,可应用于屏幕输出上。
迭代器在内部维护一个输出流对象,迭代器执行operator=运算符时实际执行的输出流的operator<<操作。
ostream_iterator关闭operator++、operator++(int)、operator*功能。
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Iterator>
class reverse_iterator //反向迭代器设计
: public iterator<typename iterator_traits<_Iterator>::iterator_category,
typename iterator_traits<_Iterator>::value_type,
typename iterator_traits<_Iterator>::difference_type,
typename iterator_traits<_Iterator>::pointer,
typename iterator_traits<_Iterator>::reference>
{
protected:
_Iterator current; //内置正向迭代器
typedef iterator_traits<_Iterator> __traits_type;
public:
typedef _Iterator iterator_type;
typedef typename __traits_type::difference_type difference_type;
typedef typename __traits_type::pointer pointer;
typedef typename __traits_type::reference reference;
reverse_iterator() : current() { }
explicit //构造函数
reverse_iterator(iterator_type __x) : current(__x) { }
reverse_iterator(const reverse_iterator& __x) //拷贝构造函数
: current(__x.current) { }
template<typename _Iter> //泛化构造函数
reverse_iterator(const reverse_iterator<_Iter>& __x)
: current(__x.base()) { }
iterator_type
base() const //取正向迭代器
{ return current; }
reference //取值运算符
operator*() const
{
_Iterator __tmp = current;
return *--__tmp; //取正向迭代器-1位置元素
}
pointer
operator->() const
{ return &(operator*()); }
reverse_iterator&
operator++() //前置++运算符
{
--current; //正向迭代器--
return *this;
}
reverse_iterator
operator++(int) //后置++运算符
{
reverse_iterator __tmp = *this;
--current; //正向迭代器--
return __tmp;
}
reverse_iterator&
operator--() //前置--运算符
{
++current; //正向迭代器++
return *this;
}
reverse_iterator
operator--(int) //后置--运算符
{
reverse_iterator __tmp = *this;
++current; //正向迭代器++
return __tmp;
}
reverse_iterator //前进+n
operator+(difference_type __n) const
{ return reverse_iterator(current - __n); } //正向迭代器-n
reverse_iterator& //+=复合运算符
operator+=(difference_type __n)
{
current -= __n; //正向迭代器-n
return *this;
}
reverse_iterator //前进-n
operator-(difference_type __n) const
{ return reverse_iterator(current + __n); } //正向迭代器+n
reverse_iterator& //-=复合运算符
operator-=(difference_type __n)
{
current += __n; //正向迭代器+n
return *this;
}
reference //下标运算符
operator[](difference_type __n) const
{ return *(*this + __n); } //迭代器自身+n,即正向迭代器-n
};
//迭代器比较(==、<、!=、>、<=、>=)运算符重载
template<typename _Iterator>
inline bool
operator==(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y)
{ return __x.base() == __y.base(); }
template<typename _Iterator>
inline bool
operator<(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y)
{ return __y.base() < __x.base(); }
template<typename _Iterator>
inline bool
operator!=(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y)
{ return !(__x == __y); }
template<typename _Iterator>
inline bool
operator>(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y)
{ return __y < __x; }
template<typename _Iterator>
inline bool
operator<=(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y)
{ return !(__y < __x); }
template<typename _Iterator>
inline bool
operator>=(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y)
{ return !(__x < __y); }
template<typename _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<typename _Iterator>
inline reverse_iterator<_Iterator>
operator+(typename reverse_iterator<_Iterator>::difference_type __n,
const reverse_iterator<_Iterator>& __x)
{ return reverse_iterator<_Iterator>(__x.base() - __n); }
template<typename _IteratorL, typename _IteratorR>
inline bool
operator==(const reverse_iterator<_IteratorL>& __x,
const reverse_iterator<_IteratorR>& __y)
{ return __x.base() == __y.base(); }
template<typename _IteratorL, typename _IteratorR>
inline bool
operator<(const reverse_iterator<_IteratorL>& __x,
const reverse_iterator<_IteratorR>& __y)
{ return __y.base() < __x.base(); }
template<typename _IteratorL, typename _IteratorR>
inline bool
operator!=(const reverse_iterator<_IteratorL>& __x,
const reverse_iterator<_IteratorR>& __y)
{ return !(__x == __y); }
template<typename _IteratorL, typename _IteratorR>
inline bool
operator>(const reverse_iterator<_IteratorL>& __x,
const reverse_iterator<_IteratorR>& __y)
{ return __y < __x; }
template<typename _IteratorL, typename _IteratorR>
inline bool
operator<=(const reverse_iterator<_IteratorL>& __x,
const reverse_iterator<_IteratorR>& __y)
{ return !(__y < __x); }
template<typename _IteratorL, typename _IteratorR>
inline bool
operator>=(const reverse_iterator<_IteratorL>& __x,
const reverse_iterator<_IteratorR>& __y)
{ return !(__x < __y); }
template<typename _IteratorL, typename _IteratorR>
inline typename reverse_iterator<_IteratorL>::difference_type
operator-(const reverse_iterator<_IteratorL>& __x,
const reverse_iterator<_IteratorR>& __y)
{ return __y.base() - __x.base(); }
template<typename _Container>
class back_insert_iterator //back_insert_iterator迭代器定义
: public iterator<output_iterator_tag, void, void, void, void>
{
protected:
_Container* container; //底层容器
public:
typedef _Container container_type; //容器类型
explicit //唯一构造函数,初始化内部容器
back_insert_iterator(_Container& __x) : container(&__x) { }
back_insert_iterator& //赋值运算符重载
operator=(const typename _Container::value_type& __value)
{
container->push_back(__value); //执行底层容器尾端插入
return *this; //返回迭代器本身
}
back_insert_iterator& //赋值运算符,参数为底层容器元素的右值引用
operator=(typename _Container::value_type&& __value)
{
container->push_back(std::move(__value));
return *this;
}
back_insert_iterator& //取值运算符
operator*()
{ return *this; } //返回迭代器本身
back_insert_iterator& //前置++运算符
operator++()
{ return *this; } //返回迭代器本身
back_insert_iterator //后置++运算符
operator++(int)
{ return *this; } //返回迭代器本身
};
template<typename _Container>
inline back_insert_iterator<_Container>
back_inserter(_Container& __x) //获得back_insert_iterator迭代器
{ return back_insert_iterator<_Container>(__x); }
template<typename _Container>
class front_insert_iterator //front_insert_iterator迭代器定义
: public iterator<output_iterator_tag, void, void, void, void>
{
protected:
_Container* container; //底层容器
public:
typedef _Container container_type; //容器类型
//构造函数,初始化底层容器,不可隐式类型转换
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=(typename _Container::value_type&& __value)
{
container->push_front(std::move(__value)); //执行底层容器的头部插入
return *this;
}
front_insert_iterator& //取值运算符
operator*()
{ return *this; } //返回迭代器本身
front_insert_iterator& //前置++运算符
operator++()
{ return *this; }
front_insert_iterator //后置++运算符
operator++(int)
{ return *this; }
};
template<typename _Container> //获得front_insert_iterator迭代器
inline front_insert_iterator<_Container>
front_inserter(_Container& __x)
{ return front_insert_iterator<_Container>(__x); }
template<typename _Container>
class insert_iterator //insert_iterator迭代器定义
: public iterator<output_iterator_tag, void, void, void, void>
{
protected:
_Container* container; //底层容器
typename _Container::iterator iter; //底层容器的迭代器
public:
typedef _Container container_type;
//构造函数,初始化底层容器及迭代器
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; //底层迭代器前进+1
return *this; //返回迭代器自身
}
insert_iterator& //赋值运算符,接受底层容器元素的右值引用
operator=(typename _Container::value_type&& __value)
{
iter = container->insert(iter, std::move(__value));
++iter;
return *this;
}
insert_iterator& //取值运算符
operator*()
{ return *this; } //返回迭代器自身
insert_iterator& //前置++运算符
operator++()
{ return *this; } //返回迭代器自身
insert_iterator& //后置++运算符
operator++(int)
{ return *this; } //返回迭代器自身
};
template<typename _Container, typename _Iterator>
inline insert_iterator<_Container> //获得inserter迭代器
inserter(_Container& __x, _Iterator __i)
{
return insert_iterator<_Container>(__x,
typename _Container::iterator(__i));
}
template<typename _Tp, typename _CharT = char,
typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
class istream_iterator //输入流迭代器设计
: public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
{
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef basic_istream<_CharT, _Traits> istream_type;
private:
istream_type* _M_stream; //内置流对象
_Tp _M_value;
bool _M_ok;
public: //默认构造函数
_GLIBCXX_CONSTEXPR istream_iterator()
: _M_stream(0), _M_value(), _M_ok(false) {}
istream_iterator(istream_type& __s) //初始化内置流对象
: _M_stream(&__s)
{ _M_read(); } //调用_M_read(),等待输入
istream_iterator(const istream_iterator& __obj)
: _M_stream(__obj._M_stream), _M_value(__obj._M_value),
_M_ok(__obj._M_ok)
{ }
const _Tp& //取值运算符
operator*() const
{
__glibcxx_requires_cond(_M_ok,
_M_message(__gnu_debug::__msg_deref_istream)
._M_iterator(*this));
return _M_value;
}
const _Tp* //取成员运算符
operator->() const { return &(operator*()); }
istream_iterator& //前置++运算符
operator++()
{
__glibcxx_requires_cond(_M_ok,
_M_message(__gnu_debug::__msg_inc_istream)
._M_iterator(*this));
_M_read(); //流对象读取数据
return *this; //返回读取后迭代器
}
istream_iterator //后置++运算符
operator++(int)
{
__glibcxx_requires_cond(_M_ok,
_M_message(__gnu_debug::__msg_inc_istream)
._M_iterator(*this));
istream_iterator __tmp = *this;
_M_read(); //流对象读取数据
return __tmp; //返回读取前迭代器
}
bool //输入流迭代器比较
_M_equal(const istream_iterator& __x) const
{ return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
private:
void
_M_read() //输入流读取数据
{
_M_ok = (_M_stream && *_M_stream) ? true : false;
if (_M_ok)
{
*_M_stream >> _M_value; //输入流读取数据到对象
_M_ok = *_M_stream ? true : false;
}
}
};
//迭代器==、!=运算符重载
template<typename _Tp, typename _CharT, typename _Traits, typename _Dist>
inline bool
operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
{ return __x._M_equal(__y); }
template <class _Tp, class _CharT, class _Traits, class _Dist>
inline bool
operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
{ return !__x._M_equal(__y); }
template<typename _Tp, typename _CharT = char,
typename _Traits = char_traits<_CharT> >
class ostream_iterator //输出流迭代器设计
: public iterator<output_iterator_tag, void, void, void, void>
{
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef basic_ostream<_CharT, _Traits> ostream_type;
private:
ostream_type* _M_stream; //内置输出流对象
const _CharT* _M_string; //输出间隔字符串
public:
ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
ostream_iterator(ostream_type& __s, const _CharT* __c)
: _M_stream(&__s), _M_string(__c) { }
ostream_iterator(const ostream_iterator& __obj)
: _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
ostream_iterator& //赋值运算符
operator=(const _Tp& __value)
{
__glibcxx_requires_cond(_M_stream != 0,
_M_message(__gnu_debug::__msg_output_ostream)
._M_iterator(*this));
*_M_stream << __value; //输出流输出对象
if (_M_string) *_M_stream << _M_string; //输出间隔字符串
return *this;
}
ostream_iterator& //取值运算符
operator*()
{ return *this; } //返回迭代器自身
ostream_iterator& //前置++运算符
operator++()
{ return *this; }
ostream_iterator& //后置++运算符
operator++(int)
{ return *this; }
};
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace