c++ std::sort() 的自定义比较函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16894700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ custom compare function for std::sort()
提问by vard
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?
. 什么是错误?
回答by Snps
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 ?trueif 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:
此外,这是一个如何std::sort
使用自定义 C++14 多态 lambda的示例:
std::sort(std::begin(container), std::end(container),
[] (const auto& lhs, const auto& rhs) {
return lhs.first < rhs.first;
});
回答by juanchopanza
std::pairalready 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 ordeingcomparison, 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
需要严格的弱排序比较,并且<=
不能满足这一点。例如,您需要<
对K
和进行小于比较V
。有了它,你所需要的就是
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;
}
回答by Jonathan Wakely
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<K,V>
,而不是const void*
。
It should return bool
not a positive or negative value. Both (bool)1
and (bool)-1
are true
so your function says everyobject is ordered before every otherobject, which is clearly impossible.
它不应返回bool
正值或负值。双方(bool)1
并(bool)-1
都true
让你的功能说每一个对象前下令所有其他对象,这显然是不可能的。
You need to model the less-than operator, not strcmp
or memcmp
style comparisons.
您需要对小于运算符、notstrcmp
或memcmp
样式比较进行建模。
See StrictWeakOrderingwhich describes the properties the function must meet.
请参阅StrictWeakOrdering,它描述了函数必须满足的属性。