我有此代码:
#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位置中得到一个子集,然后按值对它进行排序。
答案 0 :(得分:3)
operator+
只能在random access iterators上使用。 std::set::iterator
是bidirectional iterator,从中获得的反向迭代器也是如此,因为reverse iterator具有相同的迭代器类别。
要通过n
个元素推进双向迭代器,可以使用std::next
:
auto curIter = std::next(revIter, subsetSz);
请注意,对于随机访问迭代器,iter + n
是O(1)
操作,而对于双向访问迭代器,std::next(iter, n)
是O(n)
。