繁体   English   中英

为什么我不能将 operator+ 与 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 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 position 中创建一个子集,然后按值对其进行排序。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM