【问题标题】:Why I can't use operator+ with reverse_iterator?为什么我不能将 operator+ 与 reverse_iterator 一起使用?
【发布时间】:2020-08-26 06:28:46
【问题描述】:

我有这个代码:

#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cctype>
#include <string>
...
void f() {
    std::set<std::pair<int, std::string>> orderByRating;
    /*...*/
    for (auto revIter = orderByRating.rbegin(); revIter != orderByRating.rend(); revIter++) {
        int subsetSz = 1;
        auto curIter = revIter + subsetSz;
        /*...*/
    }
}

我有这个编译错误

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\xutility(1374): error C2784: 'unknown-type std::operator -(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)': could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>'
1>        with
1>        [
1>            _Ty=std::pair<int,std::string>
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\xutility(2278): note: see declaration of 'std::operator -'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\xutility(1373): note: while compiling class template member function 'std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>> std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>>::operator +(int) const'
1>        with
1>        [
1>            _Ty=std::pair<int,std::string>
1>        ]
1>c:\users\user\source\repos\consoleapplication1\consoleapplication1.cpp(57): note: see reference to function template instantiation 'std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>> std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>>::operator +(int) const' being compiled
1>        with
1>        [
1>            _Ty=std::pair<int,std::string>
1>        ]
1>c:\users\user\source\repos\consoleapplication1\consoleapplication1.cpp(53): note: see reference to class template instantiation 'std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>>' being compiled
1>        with
1>        [
1>            _Ty=std::pair<int,std::string>
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\xutility(1374): error C2784: 'unknown-type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)': could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>'
1>        with
1>        [
1>            _Ty=std::pair<int,std::string>
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\xutility(1413): note: see declaration of 'std::operator -'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\xutility(1374): error C2676: binary '-': 'const std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>' does not define this operator or a conversion to a type acceptable to the predefined operator
1>        with
1>        [
1>            _Ty=std::pair<int,std::string>
1>        ]

你能帮我理解什么是错的以及如何做对吗?

我想从当前的 reverse_iterator 位置创建一个子集,然后按值对其进行排序。

【问题讨论】:

    标签: c++ visual-studio c++11


    【解决方案1】:

    operator+ 只能用于random access iteratorsstd::set::iterator 是一个bidirectional iterator,从它获得的反向迭代器也是,因为reverse iterator 具有相同的迭代器类别。

    要通过n 元素推进双向迭代器,您可以使用std::next

    auto curIter = std::next(revIter, subsetSz);
    

    请注意,对于随机访问迭代器,iter + nO(1) 操作,而对于双向迭代器,std::next(iter, n)O(n)

    【讨论】:

      猜你喜欢
      • 2013-07-02
      • 2015-11-08
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 2011-06-05
      • 2013-08-15
      • 2017-04-04
      相关资源
      最近更新 更多