为什么我不能将运算符+与reverse_iterator一起使用?
Why I can't use operator+ with reverse_iterator?
我有这个代码:
#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 + n
是O(1)
操作,而对于双向迭代器,std::next(iter, n)
是O(n)
。
相关文章:
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将C++中的库和头与MinGW一起使用
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 将fold表达式与std::一起用于两个元组
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 将 std::allocate_shared 与多态资源分配器一起使用
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 将 exprtk 与自定义类的对象一起使用
- 将 std::set 与基于键的比较器一起使用
- 将 C++ 类与 Rcpp 一起使用,从 C 或 R 修改它
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- 如何将AERT_Allocate与 std:vector 一起使用
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 在将 new 与指针一起使用时,创建数组的指定长度