簡體   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