I want to create custom compare function for std::sort(), to sort some key-value pairs std::pair
Here is my function
template <typename K, typename V>
int comparePairs(const void* left, const void* right){
if((((pair<K,V>*)left)->first) <= (((pair<K,V>*)right)->first))
return 1;
else
return -1;
}
Then, inside some class I have vector of pairs class member:
vector<pair<K,V>> items;
And some method for sort this vector by keys, using std::sort()
std::sort(items.begin(), items.end(), comparePairs<K,V>);
I have compilation errors within , which said
"cannot convert parameter number from 'std::pair<_Ty1,_Ty2>' to 'const void*'"
. What is a mistake?
void*
s? why not asstd::pair
s? – Dominic Rodgerstd::sort
with one forqsort
, which does requireconst void *
arguments. Write a proper comparator forstd::sort
that accepts references topair<K, V>
and the problem will go away. – user4815162342