用于std :: sort()的c ++自定义比较函数

时间:2022-07-25 15:59:04

I want to create custom compare function for std::sort(), to sort some key-value pairs std::pair

我想为std :: sort()创建自定义比较函数,以对一些键值对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()按键对此向量进行排序的一些方法

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*'"

“无法将参数编号从'std :: pair <_Ty1,_Ty2>'转换为'const void *'”

. What is a mistake?

。什么是错误?

3 个解决方案

#1


24  

std::pair already has the required comparison operators, which perform lexicographical comparisons using both elements of each pair. To use this, you just have to provide the comparison operators for types for types K and V.

std :: pair已经具有所需的比较运算符,它们使用每对元素执行字典比较。要使用它,您只需要为类型K和V提供比较运算符。

Also bear in mind that std::sort requires a strict weak ordeing comparison, and <= does not satisfy that. You would need, for example, a less-than comparison < for K and V. With that in place, all you need is

还要记住,std :: sort需要严格的弱ordeing比较,并且<=不满足。例如,对于K和V,您需要一个小于比较 <对于k和v.如果到位,您需要的只是< p>

std::vector<pair<K,V>> items; 
std::sort(items.begin(), items.end()); 

If you really need to provide your own comparison function, then you need something along the lines of

如果你真的需要提供自己的比较功能,那么你需要一些东西

template <typename K, typename V>
bool comparePairs(const std::pair<K,V>& lhs, const std::pair<K,V>& rhs)
{
  return lhs.first < rhs.first;
}

#2


18  

Look here: http://en.cppreference.com/w/cpp/algorithm/sort.

请看这里:http://en.cppreference.com/w/cpp/algorithm/sort。

It says:

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
  • comp - comparison function which returns ​true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b);
  • comp - 比较函数,如果第一个参数小于第二个参数,则返回true。比较函数的签名应等于以下内容:bool cmp(const Type1&a,const Type2&b);

Also, here's an example of how you can use std::sort using a custom C++14 polymorphic lambda:

另外,这里是一个如何使用自定义C ++ 14多态lambda使用std :: sort的示例:

std::sort(std::begin(container), std::end(container),
          [] (const auto& lhs, const auto& rhs) {
    return lhs.first < rhs.first;
});

#3


10  

Your comparison function is not even wrong.

你的比较功能甚至没有错。

Its arguments should be the type stored in the range, i.e. std::pair<K,V>, not const void*.

它的参数应该是存储在范围内的类型,即std :: pair ,而不是const void *。 ,v>

It should return bool not a positive or negative value. Both (bool)1 and (bool)-1 are true so your function says every object is ordered before every other object, which is clearly impossible.

它应该返回bool而不是正值或负值。 (bool)1和(bool)-1都是正确的,所以你的函数说每个对象都在每个其他对象之前排序,这显然是不可能的。

You need to model the less-than operator, not strcmp or memcmp style comparisons.

您需要为less-than运算符建模,而不是strcmp或memcmp样式比较。

See StrictWeakOrdering which describes the properties the function must meet.

请参阅StrictWeakOrdering,它描述了函数必须满足的属性。

#1


24  

std::pair already has the required comparison operators, which perform lexicographical comparisons using both elements of each pair. To use this, you just have to provide the comparison operators for types for types K and V.

std :: pair已经具有所需的比较运算符,它们使用每对元素执行字典比较。要使用它,您只需要为类型K和V提供比较运算符。

Also bear in mind that std::sort requires a strict weak ordeing comparison, and <= does not satisfy that. You would need, for example, a less-than comparison < for K and V. With that in place, all you need is

还要记住,std :: sort需要严格的弱ordeing比较,并且<=不满足。例如,对于K和V,您需要一个小于比较 <对于k和v.如果到位,您需要的只是< p>

std::vector<pair<K,V>> items; 
std::sort(items.begin(), items.end()); 

If you really need to provide your own comparison function, then you need something along the lines of

如果你真的需要提供自己的比较功能,那么你需要一些东西

template <typename K, typename V>
bool comparePairs(const std::pair<K,V>& lhs, const std::pair<K,V>& rhs)
{
  return lhs.first < rhs.first;
}

#2


18  

Look here: http://en.cppreference.com/w/cpp/algorithm/sort.

请看这里:http://en.cppreference.com/w/cpp/algorithm/sort。

It says:

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
  • comp - comparison function which returns ​true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b);
  • comp - 比较函数,如果第一个参数小于第二个参数,则返回true。比较函数的签名应等于以下内容:bool cmp(const Type1&a,const Type2&b);

Also, here's an example of how you can use std::sort using a custom C++14 polymorphic lambda:

另外,这里是一个如何使用自定义C ++ 14多态lambda使用std :: sort的示例:

std::sort(std::begin(container), std::end(container),
          [] (const auto& lhs, const auto& rhs) {
    return lhs.first < rhs.first;
});

#3


10  

Your comparison function is not even wrong.

你的比较功能甚至没有错。

Its arguments should be the type stored in the range, i.e. std::pair<K,V>, not const void*.

它的参数应该是存储在范围内的类型,即std :: pair ,而不是const void *。 ,v>

It should return bool not a positive or negative value. Both (bool)1 and (bool)-1 are true so your function says every object is ordered before every other object, which is clearly impossible.

它应该返回bool而不是正值或负值。 (bool)1和(bool)-1都是正确的,所以你的函数说每个对象都在每个其他对象之前排序,这显然是不可能的。

You need to model the less-than operator, not strcmp or memcmp style comparisons.

您需要为less-than运算符建模,而不是strcmp或memcmp样式比较。

See StrictWeakOrdering which describes the properties the function must meet.

请参阅StrictWeakOrdering,它描述了函数必须满足的属性。