【问题标题】:Using my custom iterator with stl algorithms将我的自定义迭代器与 stl 算法一起使用
【发布时间】:2015-02-04 18:19:39
【问题描述】:

我正在尝试创建自己的迭代器,并且使用 std::generate 算法使其按预期工作。但是,当我尝试 std::find 的 std::max_element 时,我得到了一些神秘的错误。

这是我的迭代器的接口:

template <typename GridT, 
          typename GridPtr,
          typename GridRef,
          template <typename> class ShapeT>
class GridIterator
{
public:
    typedef GridIterator<GridT, GridPtr, GridRef, ShapeT> Iterator;

    // Iterator traits - typedefs and types required to be STL compliant
    typedef std::ptrdiff_t           difference_type;
    typedef typename GridT::Element  value_type;
    typedef typename GridT::Element* pointer;
    typedef typename GridT::Element& reference;
    typedef size_t                   size_type;
    std::forward_iterator_tag        iterator_category;


    GridIterator(GridT& grid,
                 ShapeT<typename GridT::Resolution> shape,
                 Index iterStartIndex);

    ~GridIterator();

    Iterator& operator++();
    Iterator  operator++(int);

    typename GridT::Element& operator*();
    typename GridT::Element* operator->();

    bool operator!=(const GridIterator& rhs) const;
    bool operator==(const GridIterator& rhs) const; 
    ....

}

使用 std::find,我得到这个错误

在 /usr/include/c++/4.6/algorithm:63:0 包含的文件中, 来自./grid/Map_Grid.h:11, 从 main.cpp:4: /usr/include/c++/4.6/bits/stl_algo.h: 在函数'_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = 地图::GridIterator, 地图::Grid*, Map::Grid&, Map::Rectangle>, _Tp = int]’: main.cpp:103:50:从这里实例化 /usr/include/c++/4.6/bits/stl_algo.h:4404:45:错误:没有匹配 调用函数 '__iterator_category(Map::GridIterator, Map::Grid*, Map::Grid&, Map::Rectangle>&)’ /usr/include/c++/4.6/bits/stl_algo.h:4404:45:注意:候选人是: /usr/include/c++/4.6/bits/stl_iterator_base_types.h:202:5:注意: 模板类型名 std::iterator_traits::iterator_category std::__iterator_category(const _Iter&)

使用 std::max_element :

在 /usr/include/c++/4.6/bits/char_traits.h:41:0 包含的文件中, 来自 /usr/include/c++/4.6/ios:41, 来自 /usr/include/c++/4.6/ostream:40, 来自 /usr/include/c++/4.6/iostream:40, 来自./grid/Map_GridIterator.h:7, 来自./grid/Map_Grid.h:8, 来自 main.cpp:4: /usr/include/c++/4.6/bits/stl_algobase.h: 在函数‘const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = 地图::GridIterator, 地图::Grid*, Map::Grid&, Map::Rectangle>]': main.cpp:102:60:
从这里/usr/include/c++/4.6/bits/stl_algobase.h:215:7 实例化: 错误:‘__a &, 常量 std::pair<_t1 _t2>&) /usr/include/c++/4.6/bits/stl_iterator.h:291:5: 注意:模板 bool std::operator&, const std::reverse_iterator<_iterator>&) /usr/include/c++/4.6/bits/stl_iterator.h:341:5: 注意:模板 bool std::operator&, const std::reverse_iterator<_iteratorr>&) /usr/include/c++/4.6/bits/stl_iterator.h:1049:5: 注意:模板 bool std::operator&, const std::move_iterator<_iteratorr>&) /usr/include/c++/4.6/bits/stl_iterator.h:1055:5: 注意:模板 bool std::operator&, const std::move_iterator<_iterator>&) /usr/include/c++/4.6/bits/basic_string.h:2510:5: 注意:模板 bool std::operator&, const std::basic_string<_chart _traits _alloc>&) /usr/include/c++/4.6/bits/basic_string.h:2522:5: 注意:模板 bool std::operator&, const _CharT*) /usr/include/c++/4.6/bits/basic_string.h:2534:5: 注意:模板 bool std::operator&) /usr/ include/c++/4.6/bits/stl_vector.h:1290:5: 注意:模板 bool std::operator&, const std::vector<_tp _alloc>&) /usr/include/c++/4.6/tuple:586:5: 注意:模板 bool std::operator&, const std::tuple<_elements ...>&)

【问题讨论】:

    标签: c++ templates stl iterator stl-algorithm


    【解决方案1】:

    您缺少用于声明指示迭代器类别的别名的 typedef 关键字:

    // Iterator traits - typedefs and types required to be STL compliant
    //...
    typedef std::forward_iterator_tag iterator_category;
    ~~~~~~^
    

    没有typedef,你实际上是在声明一个数据成员。

    为避免此类错误,您可以使用std::iterator 类模板作为基类,而不是自己定义这些别名:

    class GridIterator : public std::iterator<std::forward_iterator_tag
                                            , typename GridT::Element>
    

    【讨论】:

    • 完美,成功了!我将更改为将其用作基类。谢谢!
    • 它适用于 std::find,但不适用于 std:max_elememt。最后一条错误消息仍然存在。
    • @q-bertsuit 迭代器指向的类型必须小于可比较,除非您为 max_element 提供自定义比较器
    猜你喜欢
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 2014-05-23
    • 2010-10-10
    • 1970-01-01
    相关资源
    最近更新 更多