reverse_iterator适配器 [英] reverse_iterator adapter
问题描述
我正在尝试为我的迭代器和const_iterator类实现一个反向迭代器适配器,但有点麻烦。如果有人可以指导我完成这个,那将非常感谢!
I'm trying to implement a reverse-iterator adaptor for my iterator and const_iterator classes with a little bit of trouble. If anyone could guide me through this, that would be greatly appreciated!
我的想法是我应该能够从我的rbegin()和rend创建一个反向迭代器()函数调用
The idea is that I should be able to create a reverse-iterator from my rbegin() and rend() function calls
reverse_iterator rbegin();
reverse_iterator rend();
const_reverse_iterator rbegin() const;
const_reverse_iterator rend() const;
我在课堂上使用以下typedef:
I'm using the following typedef's in the class:
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;
如您所见,我希望能够使用模板创建反向迭代器, reverse_iterator类是迭代器还是const_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.
不幸的是,这是我坚持的......
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...
由于我可以是迭代器或const_iterator,因此引用和指针的typedef在两个类之间有所不同。不编译的行是:
Since I can be an iterator or a const_iterator, the typedef of reference and pointer vary between the two classes. The lines that aren't compiling are these:
I::reference operator*() const;
I::pointer operator->() const;
我不知道如何让一个reverse_iterator类同时用于iterator和const_iterator我不能做I :: reference和I :: pointer。我也尝试在前面添加模板,因为它们在迭代器类(例如)中被定义为:
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 also tried adding template in front of those, since they are defined in the iterator class (for example) as:
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;
此外,构造函数应该只接受 I
。
In addition, the constructor should accept just the I
.
但是,根本不需要写这个类。标准库具有 reverse_iterator 。或者如果您对此不满意,还可以 Boost。 ReverseIterator 。
However, there is no need to write this class at all. The standard library has reverse_iterator for this. Or if you are not happy with that, there's also Boost.ReverseIterator.
所需要的只是
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
这篇关于reverse_iterator适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!