简体   繁体   English

reverse_iterator适配器

[英]reverse_iterator adapter

I'm trying to implement a reverse-iterator adaptor for my iterator and const_iterator classes with a little bit of trouble. 我正在尝试为我的迭代器和const_iterator类实现反向迭代器适配器,但会有些麻烦。 If anyone could guide me through this, that would be greatly appreciated! 如果有人可以指导我完成此工作,将不胜感激!

The idea is that I should be able to create a reverse-iterator from my rbegin() and rend() function calls 我的想法是我应该能够从我的rbegin()和rend()函数调用中创建一个反向迭代器

reverse_iterator rbegin();
reverse_iterator rend();
const_reverse_iterator rbegin() const;
const_reverse_iterator rend() const;

I'm using the following typedef's in the class: 我在该类中使用以下typedef:

typedef btree_iterator<T> iterator;
typedef const_btree_iterator<T> const_iterator;
typedef reverse_btree_iterator<iterator> reverse_iterator;
typedef reverse_btree_iterator<const_iterator> const_reverse_iterator;

As you can see, I would like to be able to create reverse-iterators using templates, giving the reverse_iterator class either an iterator or const_iterator. 如您所见,我希望能够使用模板创建反向迭代器,为reverse_iterator类提供迭代器或const_iterator。

Unfortunately, it is this bit I'm stuck on... 不幸的是,这就是我所坚持的...

Below is the class definition that I currently have, with errors. 下面是我当前拥有的类定义,但有错误。

template <typename I> class reverse_btree_iterator {

  typedef ptrdiff_t                     difference_type;
  typedef bidirectional_iterator_tag    iterator_category;

public:

  reverse_btree_iterator() : base_(I()) {}
  template <typename T> reverse_btree_iterator(const btree_iterator<T>& rhs) : base_(rhs) {}

  I base() { return base_; }

  I::reference operator*() const;
  I::pointer operator->() const;
  I& operator++();
  I operator++(int);
  I& operator--();
  I operator--(int);
  bool operator==(const I& other) const;
  bool operator!=(const I& other) const;

private:

  I base_;

};

I've never used templates like this before, so it is very likely I'm completely misunderstanding how they can be used... 我以前从未使用过这样的模板,因此很可能我完全误解了如何使用它们。

Since I can be an iterator or a const_iterator, the typedef of reference and pointer vary between the two classes. 由于我可以是迭代器或const_iterator,因此引用和指针的typedef在这两个类之间有所不同。 The lines that aren't compiling are these: 未编译的行如下:

I::reference operator*() const;
I::pointer operator->() const;

I'm not sure how else I can make the one reverse_iterator class work for both iterator and const_iterator if I'm not able to do I::reference and I::pointer. 如果不能执行I :: reference和I :: pointer,我不确定如何使一个reverse_iterator类同时适用于迭代器和const_iterator。 I also tried adding template in front of those, since they are defined in the iterator class (for example) as: 我还尝试在这些模板的前面添加模板,因为它们在iterator类(例如)中定义为:

typedef T*                            pointer;
typedef T&                            reference;

reference and pointer are dependent names, so you have to use 引用和指针是从属名称,因此您必须使用

typename I::reference operator*() const;
typename I::pointer operator->() const;

In addition, the constructor should accept just the I . 此外,构造函数应仅接受I

However, there is no need to write this class at all. 但是,根本不需要编写此类。 The standard library has reverse_iterator for this. 标准库为此提供了reverse_iterator Or if you are not happy with that, there's also Boost.ReverseIterator . 或者,如果您对此不满意,还可以使用Boost.ReverseIterator

All it takes is just 它只需要

typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

In addition, you forgot to provide comparison operators with other reverse iterators of the same type. 此外,您忘记为比较运算符提供其他相同类型的反向迭代器。 This is a reverse iterator requirement. 这是反向迭代器的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM