使用迭代器和const_iterator取消引用运算符

时间:2015-10-26 14:48:42

标签: c++ iterator

我一直在努力解决与开发模板类迭代器相关的问题。更具体地说,以正确的方式实现解除引用运算符(operator *()),以便模板类涵盖迭代器和const_iterator的情况。我确信我错过了一些明显的东西,但我看不到它。你能帮帮我吗?

假设我有以下模板类(Iterator.hpp),我想用它来迭代将STL容器(std :: map,std :: vector)包装为私有成员的类的对象。 / p>

#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;
};

现在假设我有一个主(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";

    }


}

如果我尝试编译此代码:g++ example.cpp -std=c++11 -stdlib=libc++。我会收到以下错误:

./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.

我认为通过简单地使用operator*()模板中的const和非const版本重载Iterator.hpp就足够了,但显然情况并非如此。你知道我在这里缺少什么吗?对此问题的任何帮助将不胜感激。

非常感谢!

1 个答案:

答案 0 :(得分:0)

const_iteratoriterator是不同的类型,应该作为单独的类型实现。 const_iterator应该在它的dereference运算符中返回一个const引用,normal iterator返回可修改的引用。

您不应该将const_iteratorconst iterator混淆。