gpt4 book ai didi

c++ - 将我的自定义迭代器与 STL 算法一起使用

转载 作者:行者123 更新时间:2023-11-30 01:16:05 26 4
gpt4 key购买 nike

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

这是我的迭代器的接口(interface):

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,我得到这个错误:

In file included from /usr/include/c++/4.6/algorithm:63:0,from ./grid/Map_Grid.h:11,from main.cpp:4: /usr/include/c++/4.6/bits/stl_algo.h: In function ‘_IIterstd::find(_IIter, _IIter, const _Tp&) [with _IIter =Map::GridIterator<Map::Grid<double, int>, Map::Grid<double, int>,Map::Grid<double, int>&, Map::Rectangle>, _Tp = int]’:main.cpp:103:50: instantiated from here/usr/include/c++/4.6/bits/stl_algo.h:4404:45: error: no matchingfunction for call to‘__iterator_category(Map::GridIterator<Map::Grid<double, int>,Map::Grid<double, int>, Map::Grid<double, int>&, Map::Rectangle>&)’/usr/include/c++/4.6/bits/stl_algo.h:4404:45: note: candidate is:/usr/include/c++/4.6/bits/stl_iterator_base_types.h:202:5: note:template typename std::iterator_traits::iterator_categorystd::__iterator_category(const _Iter&)

使用 std::max_element :

In file included from /usr/include/c++/4.6/bits/char_traits.h:41:0,from /usr/include/c++/4.6/ios:41,from /usr/include/c++/4.6/ostream:40,from /usr/include/c++/4.6/iostream:40,from ./grid/Map_GridIterator.h:7,from ./grid/Map_Grid.h:8,from main.cpp:4: /usr/include/c++/4.6/bits/stl_algobase.h: In function ‘const _Tp&std::max(const _Tp&, const _Tp&) [with _Tp =Map::GridIterator<Map::Grid<double, int>, Map::Grid<double, int>,Map::Grid<double, int>&, Map::Rectangle>]’: main.cpp:102:60:instantiated from here /usr/include/c++/4.6/bits/stl_algobase.h:215:7:error: no match for ‘operator<’ in ‘__a < __b’/usr/include/c++/4.6/bits/stl_algobase.h:215:7: note: candidates are:/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1,class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&,const std::pair<_T1, _T2>&)/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template bool std::operator<(const std::reverse_iterator<_Iterator>&, conststd::reverse_iterator<_Iterator>&)/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class_IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, conststd::reverse_iterator<_IteratorR>&)/usr/include/c++/4.6/bits/stl_iterator.h:1049:5: note: template<class_IteratorL, class _IteratorR> bool std::operator<(const std::move_iterator<_IteratorL>&, conststd::move_iterator<_IteratorR>&)/usr/include/c++/4.6/bits/stl_iterator.h:1055:5: note: template bool std::operator<(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)/usr/include/c++/4.6/bits/basic_string.h:2510:5: note: template<class_CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, conststd::basic_string<_CharT, _Traits, _Alloc>&)/usr/include/c++/4.6/bits/basic_string.h:2522:5: note: template<class_CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT)/usr/include/c++/4.6/bits/basic_string.h:2534:5: note: template<class_CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&) /usr/include/c++/4.6/bits/stl_vector.h:1290:5: note: template<class_Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&) /usr/include/c++/4.6/tuple:586:5: note: template<class ... _TElements,class ... _UElements> bool std::operator<(const std::tuple<_TElements...>&, const std::tuple<_Elements ...>&)

最佳答案

您缺少用于声明指示迭代器类别的别名的 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>

关于c++ - 将我的自定义迭代器与 STL 算法一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27335210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com