为什么我不能将运算符+与reverse_iterator一起使用?

Why I can't use operator+ with reverse_iterator?

本文关键字:iterator 一起 reverse 不能 运算符 为什么      更新时间:2023-10-16

我有这个代码:

#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 studio2017communityvctoolsmsvc14.12.25827includexutility(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 studio2017communityvctoolsmsvc14.12.25827includexutility(2278): note: see declaration of 'std::operator -'
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.12.25827includexutility(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:usersusersourcereposconsoleapplication1consoleapplication1.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:usersusersourcereposconsoleapplication1consoleapplication1.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 studio2017communityvctoolsmsvc14.12.25827includexutility(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 studio2017communityvctoolsmsvc14.12.25827includexutility(1413): note: see declaration of 'std::operator -'
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.12.25827includexutility(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位置创建一个子集,然后按值对其进行排序。

operator+只能在随机访问迭代器上使用。std::set::iterator是双向迭代器,从中获取的反向迭代器也是如此,因为反向迭代器具有相同的迭代器类别。

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

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

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