[英]Dereference operator with iterator and const_iterator
I have been struggling with a problem related to developing a template class iterator. 我一直在努力解决与开发模板类迭代器有关的问题。 More specifically in the proper way to implement the dereferencing operator (operator*()) so that the template class covers the case for both iterator and const_iterator.
更具体地说,以实现解引用运算符(operator *())的正确方式,使模板类涵盖iterator和const_iterator的情况。 I am sure I am missing something obvious here but I cannot see it.
我确定我在这里缺少明显的东西,但看不到。 Could you help me?
你可以帮帮我吗?
Suppose I have the following template class ( Iterator.hpp
) that I want to use to iterate over objects of classes that wrap a STL container (std::map, std::vector) as a private member. 假设我要使用以下模板类(
Iterator.hpp
)来遍历将STL容器(std :: map和std :: vector)包装为私有成员的类的对象。
#include <iostream>
#include <iterator>
template<typename iterator_type>
class Iterator
{
public:
/** \brief Type to be returned when de-referencing the iterator*/
typedef typename std::iterator_traits<iterator_type>::value_type value_type;
/** \brief Constructor*/
inline Iterator(iterator_type i) : iterator(i) {}
/** \brief Dereference operator */
inline value_type& operator*() {return *iterator;}
inline const value_type& operator*() const {return *iterator;}
/** \brief Increment operator */
inline Iterator & operator++() {++iterator; return *this;}
/** \brief Inequality operator */
inline bool operator!=(const Iterator & right) const
{return iterator != right.iterator;}
/** \brief Inequality operator */
inline bool operator!=(const iterator_type & right) const
{return iterator != right;}
/** \brief Distance between iterators */
inline int operator-(const Iterator & right) const
{return std::distance(right.iterator, iterator);}
/** \brief Distance between iterators */
inline int operator-(const iterator_type & right) const
{return std::distance(right, iterator);}
private:
/** \brief Internal member, of iterator type*/
iterator_type iterator;
};
Now suppose I have a main ( example.cpp
) that creates a vector of ints and tries to print them out using a const_iterator
, like so: 现在假设我有一个main(
example.cpp
),它创建一个int向量,并尝试使用const_iterator
将它们打印出来,如下所示:
#include "Iterator.hpp"
#include <vector>
typedef std::vector<int> IntVector;
int main(int argc, char* argv[]) {
unsigned int nElements(10);
IntVector intVector(nElements);
for (unsigned int i = 0; i < nElements; ++i) {
intVector[i] = i;
}
std::cerr << " Printout of the vector \n";
Iterator<IntVector::const_iterator> it(intVector.begin());
for(; it != intVector.end(); ++it) {
std::cerr << *it << "\n";
}
}
If I try to compile this code: g++ example.cpp -std=c++11 -stdlib=libc++
. 如果我尝试编译此代码:
g++ example.cpp -std=c++11 -stdlib=libc++
。 I will get the following error: 我将收到以下错误:
./Iterator.hpp:16:48: error: binding of reference to type 'value_type' (aka 'int') to a value of type 'const int' drops
qualifiers
inline value_type& operator*() {return *iterator;}
^~~~~~~~~
example.cpp:19:22: note: in instantiation of member function 'Iterator<std::__1::__wrap_iter<const int *> >::operator*'
requested here
std::cerr << *it << "\n";
^
1 error generated.
I thought that by simply overloading the operator*()
with the const and non-const versions as done in the Iterator.hpp
template would suffice, but that is obviously not the case. 我认为,只需像
Iterator.hpp
模板中那样,通过用const和非const版本重载operator*()
,但这显然不是这种情况。 Do you know what I am missing here? 你知道我在这里想念的吗? any help on this issue would be greatly appreciated.
在这个问题上的任何帮助将不胜感激。
Many thanks! 非常感谢!
const_iterator
and iterator
are different types, and should be implemented as separate types. const_iterator
和iterator
是不同的类型,应将其实现为单独的类型。 const_iterator should return a const reference in it's dereference operator, normal iterator returns modifiable reference. const_iterator应该在其解引用运算符中返回const引用,普通迭代器返回可修改的引用。
You should not confuse const_iterator
with const iterator
. 您不应将
const_iterator
与const iterator
混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.