简体   繁体   中英

C++ STL .. how does custom comparator work?

I don't understand how the custom comparator works in C++ .. whats really happening behind the scene?

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 . In which order are the elements of array passed to comp() at run time... is it like-->

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. 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.

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.

A function that defines a strict weak ordering is similar to the usual less than ( < ) operator:

  1. For all x , x < x is false.
  2. For all x and y , if x < y is true, y < x is false.
  3. For all x , y , and z , if x < y and y < z are both true, x < z must be true.

For the ease of reading, I'm using x < y in place of comp(x, y) .

Save the standard C library qsort () that can be used to sort the matrix. As the name suggests, the function uses the QuickSort algorithm to sort the specified array. The following is a sample of qsort ()

void qsort (void * base, size_t num, size_t size, int (* comparator) (const void *, const void *)); The main point about qsort () is the comparison function comparison. 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). 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)

Internal comparison (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 The item referenced by p1 moves after the item referenced by p2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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