编译代码时safe_iterator.h 中发生错误

Error happening in safe_iterator.h when I compile my code

本文关键字:错误 iterator 代码 safe 编译      更新时间:2023-10-16

好吧,伙计们。我知道我已经发布了这段代码(我现在已经从以前修复了它),但似乎仍然有一些事情发生,我不想编辑我以前的帖子以包含另一个问题(因为这违反了规则)。

当我编译下面的代码时,我收到来自库深处的错误消息

safe_iterator.h: **error: no match for 'operator<' in '(+ __lhs)->__gnu_debug::_Safe_iterator<_Iterator, _Sequence>::base [with _Iterator = std::_Rb_tree_iterator<std::pair<const int, int> >, _Sequence = __gnu_debug_def::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >]() < (+ __rhs)->__gnu_debug::_Safe_iterator<_Iterator, _Sequence>::base [with _Iterator = std::_Rb_tree_iterator<std::pair<const int, int> >, _Sequence = __gnu_debug_def::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >]()'**

有什么想法吗?请不要对我太苛刻。我有点n00b。:)

// Returns the mode (most common element) of an integer array
int mode(int* arrPtr, int size) { 
    assert (size > 0);
    std::map<int,int> M; 
    for (int k = 0; k < size; ++k) 
        M[arrPtr[k]]++;
    std::pair<int,int> maxpair(M.begin()->first, M.begin()->second);
    for (std::map<int,int>::iterator it = M.begin() + 1; it < M.end(); ++it) 
        if (it->second > maxpair.second) maxpair = *it;
    return (maxpair.first);
}

迭代器不实现operator<

你应该使用it != end,当心size为零!