简体   繁体   English

C ++ STL ..定制比较器如何工作?

[英]C++ STL .. how does custom comparator work?

I don't understand how the custom comparator works in C++ .. whats really happening behind the scene? 我不了解自定义比较器如何在C ++中工作..幕后到底发生了什么?

Lets say there are two arguments x and y to our comparator comp(x,y) and we use this comparator function for let's say a vector vector<int> v (size) in something like std::sort() or for a priority_queue . 假设我们的比较器comp(x,y)有两个参数xy ,我们使用此比较器函数来表示类似std::sort()的vector vector<int> v (size) std::sort()priority_queue In which order are the elements of array passed to comp() at run time... is it like--> 运行时传递给comp()的数组元素的顺序是comp()

comp(v[0],v[1]) 
comp(v[1],v[2])

For any kind of custom comparison what are the rules? 对于任何类型的自定义比较,规则是什么? I have heard about the weak ordering concept, can anyone explains this a little clearly please? 我听说过弱订购概念,请问有人可以清楚地解释一下吗?

From here : 这里

comp

Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. 二进制函数,该函数接受范围内的两个元素作为参数,并返回可转换为bool的值。 The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines. 返回的值指示是否按其定义的特定严格弱顺序将作为第一个参数传递的元素视为在第二个参数之前。

The function shall not modify any of its arguments. 该函数不得修改其任何参数。

This can either be a function pointer or a function object. 这可以是一个函数指针或一个函数对象。

In other words, comp(x,y) should return true if x is supposed to be placed before y in the resulting vector. 换句话说,如果应该在结果向量中将x放置在y之前,则comp(x,y)应该返回true。

Edit: I think I misread your question. 编辑:我想我读错了你的问题。 The order that the vector elements get passed to the comparator depends on the algorithm used, which is implementation-dependent since the standard doesn't specify an exact algorithm. 矢量元素传递到比较器的顺序取决于所使用的算法,这取决于实现,因为标准没有指定确切的算法。 I believe quicksort is a common algorithm for it. 我相信快速排序是一种常见的算法。

There is no guarantee on how the comparison function is used, but it has to satisfy a strict weak ordering , besides other properties pointed out in user9549915's answer. 无法保证如何使用比较函数,但是除了user9549915的答案中指出的其他属性外,它还必须满足严格的弱排序

A function that defines a strict weak ordering is similar to the usual less than ( < ) operator: 定义严格弱排序的函数类似于通常的小于( < )运算符:

  1. For all x , x < x is false. 对于所有xx < x为假。
  2. For all x and y , if x < y is true, y < x is false. 对于所有xy ,如果x < y为true,则y < x为false。
  3. For all x , y , and z , if x < y and y < z are both true, x < z must be true. 对于所有xyz ,如果x < yy < z都为真,则x < z必须为真。

For the ease of reading, I'm using x < y in place of comp(x, y) . 为了便于阅读,我使用x < y代替comp(x, y)

Save the standard C library qsort () that can be used to sort the matrix. 保存可用于对矩阵进行排序的标准C库qsort()。 As the name suggests, the function uses the QuickSort algorithm to sort the specified array. 顾名思义,该函数使用QuickSort算法对指定的数组进行排序。 The following is a sample of qsort () 以下是qsort()的示例

void qsort (void * base, size_t num, size_t size, int (* comparator) (const void *, const void *)); void qsort(void * base,size_t num,size_t size,int(*比较器)(const void *,const void *)); The main point about qsort () is the comparison function comparison. 关于qsort()的要点是比较函数比较。 The comparison function takes two arguments and contains a logic to determine its relative order in the output that has been sorted. 比较函数采用两个参数,并包含确定已排序输出中其相对顺序的逻辑。 The idea is to provide flexibility so that qsort () can be used for any type (including user-defined types) and can be used to get any desired order (increase or decrease or anything else). 这个想法是为了提供灵活性,以便qsort()可以用于任何类型(包括用户定义的类型),并且可以用于获取任何所需的顺序(增加或减少,或其他任何方式)。 The comparison function took two indices as a size (both bound to the const constellation *) and determined the order of elements to return (in a constant and multiple way) 比较函数将两个索引作为大小(都绑定到const星座*),并确定要返回的元素顺序(以常数和多重方式)

Internal comparison (const void * p1, const void * p2); 内部比较(const void * p1,const void * p2); Value return meaning <0 The item referenced by p1 goes before the element referenced by p2 0 The element referred to in p1 is equal to the element referred to in p2 返回值的含义<0由p1引用的项目位于p2引用的元素之前0 p1引用的元素等于p2引用的元素

0 The item referenced by p1 moves after the item referenced by p2 0 p1引用的项目在p2引用的项目之后移动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM