[英]C++ custom collection reverse_iterator with similar behaviour to std::vector implementation
I have a template based custom collection (as we cannot use std::vector on the interface). 我有一个基于模板的自定义集合(因为我们不能在接口上使用std :: vector)。 I would like to implement a reverse_iterator specific to this collection.
我想实现特定于此集合的reverse_iterator。 The reverse iterator struct below is a structure nested within the collection class.
下面的反向迭代器结构是嵌套在集合类中的结构。 An iterator (basically a pointer to element type of the collection) is already implemented.
迭代器(基本上是指向集合元素类型的指针)已经实现。 This is my first attempt at a reverse iterator.
这是我第一次尝试反向迭代器。
template <typename T>
struct reverse_iterator
{
typedef T::iterator iterator;
typedef T& reference;
inline reverse_iterator(const iterator & it):_it(it){}
inline reverse_iterator() : _it(0x0) {}
inline iterator base() const {iterator it = _it; return --it;}
inline reverse_iterator operator ++ () {return reverse_iterator(--_it);}
inline reverse_iterator operator -- () {return reverse_iterator(++_it);}
inline reverse_iterator operator ++ (int val) {_it -= val; return reverse_iterator(_it);}
inline reverse_iterator operator -- (int val) {_it += val; return reverse_iterator(_it);}
inline reverse_iterator operator += (int val) {_it -= val; return reverse_iterator(_it);}
inline reverse_iterator operator -= (int val) {_it += val; return reverse_iterator(_it);}
inline reverse_iterator operator + (int val) const {iterator it = _it - val; return reverse_iterator(it);}
inline reverse_iterator operator - (int val) const {iterator it = _it + val; return reverse_iterator(it);}
bool operator == (const iterator & other) const {return other == base();}
bool operator != (const iterator & other) const {return other != base();}
reference operator*() const {return *base();}
iterator operator->() const {return base();}
private:
iterator _it;
};
Except for the things mentioned below your implementation is almost the same as the implementation in libstdc++ ( v3 , but still somewhat accurate). 除了下面提到的内容外,您的实现与libstdc ++中的实现几乎相同( v3 ,但仍然有些准确)。 Note that you're currently missing all non-member functions.
请注意,您当前缺少所有非成员函数。 All in all you should try to match the
std::reverse_iterator
interface: if you're ever able to use std types you can happily exchange your mylab::reverse_iterator
by std::reverse_iterator
. 总之,您应该尝试匹配
std::reverse_iterator
接口:如果您能够使用std类型,则可以通过std::reverse_iterator
来愉快地交换mylab::reverse_iterator
std::reverse_iterator
。
reverse_iterator
, such as operator==
, operator!=
, operator<
and so on. reverse_iterator
之间的所有比较运算符,例如operator==
, operator!=
, operator<
等等。 This is basically a list of stuff where your reverse_iterator
differs from the standard one. 这基本上是清单,其中您的
reverse_iterator
与标准清单有所不同。
Usually the pre-increment/-decrement operators return a reference ( *this
) and not a new object. 通常,预增/减运算符返回一个引用(
*this
),而不是一个新对象。
The post increment/decrement operators shouldn't take a value: 增/减后运算符不应采用以下值:
inline reverse_iterator operator ++ (int) { reverse_iterator tmp = *this; ++*this; // implement post-increment in terms of pre-increment! // or --_it; return tmp; } inline reverse_iterator operator -- (int) { ... }
The compound assignment operators also usually return references. 复合赋值运算符通常还返回引用。
const iterator&
constructor should be explicit
, otherwise one could accidentally mix reverse and normal iterators. const iterator&
构造函数应该是explicit
,否则可能会意外地混合使用反向迭代器和正常迭代器。 Instead of a container type T
you should use the underlying iterator as template parameter: 应该使用基础迭代器作为模板参数,而不是容器类型
T
:
template <typename Iterator> struct reverse_iterator { typedef Iterator iterator; typedef typename iterator_traits<Iterator>::reference reference; ... }
This enables you to use reverse_iterator
on anything that iterator_traits
can handle: 这使您可以在
iterator_traits
可以处理的任何事情上使用reverse_iterator
:
template <class Iterator> struct iterator_traits{ typedef typename Iterator::reference reference; // Add other things }; template <class T> struct iterator_traits<T*>{ typedef T & reference; };
With this you can even use reverse_iterator<int *>
or similar. 这样,您甚至可以使用
reverse_iterator<int *>
或类似的方法。
operator->
usually returns a pointer to the underlying object, not an intermediary iterator. operator->
通常返回指向基础对象的指针,而不是中间迭代器。 You might want to add a pointer
typedef to both your traits and your original iterator. 您可能想要为特征和原始迭代器添加
pointer
typedef。
operator==(const iterator&)
. operator==(const iterator&)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.