【问题标题】:Dereference operator with iterator and const_iterator带有迭代器和 const_iterator 的取消引用运算符
【发布时间】:2016-01-25 17:25:06
【问题描述】:

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

假设我有以下模板类 (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;
};

现在假设我有一个 main (example.cpp),它创建一个整数向量并尝试使用 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.

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

非常感谢!

【问题讨论】:

  • 使用typename std::iterator_traits&lt;iterator_type&gt;::reference 代替value_type&amp;
  • 对于 *it 选择 const 版本,迭代器 it 本身必须是 const,而不是您尝试访问的值。

标签: c++ iterator


【解决方案1】:

const_iteratoriterator 是不同的类型,应该作为不同的类型来实现。 const_iterator 应该在它的解引用操作符中返回一个 const 引用,普通的迭代器返回可修改的引用。

您不应将const_iteratorconst iterator 混淆。

【讨论】:

    猜你喜欢
    • 2021-06-10
    • 2013-12-17
    • 1970-01-01
    • 2017-11-26
    • 2015-03-08
    • 2011-07-03
    • 2011-09-28
    • 2011-12-07
    • 2011-10-18
    相关资源
    最近更新 更多