I have this code:
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cctype>
#include <string>
...
void f() {
std::set<std::pair<int, std::string>> orderByRating;
/*...*/
auto revIter = orderByRating.rbegin();
int subsetSz = 2;
auto cmp = [](std::pair<int, std::string> const & a, std::pair<int, std::string> const & b) {
return a.second < b.second;
};
std::sort(revIter, std::next(revIter, subsetSz), cmp);
}
And I get this error
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): error C2679: binary '-': no operator found which takes a right-hand operand of type 'const std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>>' (or there is no acceptable conversion)
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(1383): note: could be '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:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): note: while trying to match the argument list '(const std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>>, const std::reverse_iterator<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:\users\user\source\repos\consoleapplication1\consoleapplication1.cpp(66): note: see reference to function template instantiation 'void std::sort<std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>>,topCompetitors::<lambda_885448e6a7e9e4442c4f55247636e75d>>(_RanIt,_RanIt,_Pr)' being compiled
1> with
1> [
1> _Ty=std::pair<int,std::string>,
1> _RanIt=std::reverse_iterator<std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<int,std::string>>>>>,
1> _Pr=topCompetitors::<lambda_885448e6a7e9e4442c4f55247636e75d>
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): error C2672: '_Sort_unchecked': no matching overloaded function found
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3212): error C2780: 'void std::_Sort_unchecked(_RanIt,_RanIt,_Diff,_Pr)': expects 4 arguments - 3 provided
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\algorithm(3173): note: see declaration of 'std::_Sort_unchecked'
Can you help me to understand what's wrong and how to fix it? I want to sort by value subset from set
It means that I can sort only on random_iterator?
Or in that case are there any std methods to extract subset values to make vector out of them?