#include <algorithm>
#include <iostream>
#include <vector>
using std:: uint32_t ;
template < class RandomAccessIterator,class Compare >
auto sort2( RandomAccessIterator begin,RandomAccessIterator end,Compare cmp) - >
std:: vector < std:: pair < uint32_t ,RandomAccessIterator>>
{
using valueType= typename std:: iterator_traits < RandomAccessIterator> :: value_type ;
using Pair= std:: pair < uint32_t ,RandomAccessIterator> ;
std:: vector < Pair> index_pair;
index_pair.reserve ( std:: distance ( begin,end) ) ;
for ( uint32_t idx= 0 ; begin! = end; ++ begin,++ idx) {
index_pair.push_back ( Pair( idx,begin) ) ;
}
std:: sort ( index_pair.begin ( ) ,index_pair.end ( ) ,[ & ] ( const Pair& lhs,const Pair& rhs) {
return cmp( * lhs.second ,* rhs.second ) ;
} ) ;
return index_pair;
}
int main( )
{
std:: vector < int > a{ 1 ,7 ,3 ,4 ,8 ,4 } ;
auto sorted_pair= sort2( a.begin ( ) ,a.end ( ) , std:: greater < int > ( ) ) ;
for ( uint32_t i= 0 ; i< a.size ( ) ; ++ i) {
std:: cout << "Last position : " << sorted_pair[ i] .first <<
"\t Value : " << * sorted_pair[ i] .second << '\n ' ;
}
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgc3RkOjp1aW50MzJfdDsKdGVtcGxhdGU8IGNsYXNzIFJhbmRvbUFjY2Vzc0l0ZXJhdG9yLGNsYXNzIENvbXBhcmUgPgphdXRvIHNvcnQyKFJhbmRvbUFjY2Vzc0l0ZXJhdG9yIGJlZ2luLFJhbmRvbUFjY2Vzc0l0ZXJhdG9yIGVuZCxDb21wYXJlIGNtcCkgLT4KICAgc3RkOjp2ZWN0b3I8c3RkOjpwYWlyPHVpbnQzMl90LFJhbmRvbUFjY2Vzc0l0ZXJhdG9yPj4KewogICAgdXNpbmcgdmFsdWVUeXBlPXR5cGVuYW1lIHN0ZDo6aXRlcmF0b3JfdHJhaXRzPFJhbmRvbUFjY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