You are not logged in. Please login at www.codechef.com to post your questions!

×

# Custom Comparators

 0 Can anyone guide me how to use custom comparators while sorting vector or in set/map? I'm a bit confuse about the method's signatures and parameters(when to use const and pointer), also when to use less than sign(<) and greater than sign(>)? Thank in advance. asked 01 Feb '15, 13:42 2★a_freak 1●1●1●2 accept rate: 0%

One Answer:
 0 I'll start with qsort. It is a function that can sort any type of data. You have to provide with it: a base pointer, which points to the start of a contiguous block of data, the number of elements in the block, the size of one data member, and a function that compares two data values. Since a sorting algorithm in general doesn't depend upon the type of the data being sorted, qsort() can be written without the knowledge of what data types it is sorting. But to be able to do that, qsort() takes void * parameters, which means "generic pointer" in C. EX: qsort(data,n, sizeof(int),compare);  Here the sorting of array data which has N integer elements will be done. Compare function will be called for every pair from &data[0] to &data[n-1]. This compare function takes two const void * parameters and returns int. While writing this function,ex: int compare(const void *a, const void *b) { const int* p= (int*)a; const int *q= (int*)b; // do whatever you want here // Usually the convention is, let there are two pointers as arguments to the function. The function //returns a value less than 0, if The element pointed by p1 goes before the element pointed by p2. 0 if The //element pointed by p1 is equivalent to the element pointed by p2 and >0 if The element pointed by p1 goes after the element pointed by p2 }  We know that the pointers are passed as int but disguised as void *, thus we need to typecast them. which is done in the first two lines of the function. You may be thinking that why we need to write void * when we know we have integer arguments but think when we have structure nodes. We need the typecast then!!See this: typedef struct node { int id; char foo[255];  } Node; int compare(const void * x, const void * y) { Node * a = (Node *) x; Node * b = (Node *) y; return a->id - b->id; } int main() { Node array[SIZE]; int n; ... qsort(array, n, sizeof(Node), compare); ... return 0; }  Now whenever this qsort is called, compiler doesnot have type information about the comparisons it'll be making, thus it needs to do that typecast thing everytime. I think you can for now understand that this can make the execution slow, since for every pair it needs to typecase, so extra overhead. Let's move on to sort() now, sort(m.begin(), m.end(),comp)); // m is the map here  comp function: 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. bool comp(int i, int j) { return (i) it'll sort in non-increasing order? Also please tell,how to use custom comparator while inserting values into priority queue,etc. (01 Feb '15, 18:40) a_freak2★ Yes, < will sort in increasing or accurately non-decreasing order and reversing it to > will sort in non-increasing. (01 Feb '15, 20:05) damn_me3★ Can you tell me how to use custom comparator while inserting values into priority queue,etc? (01 Feb '15, 21:49) a_freak2★ I'll update the answer with the simple code and explanation of that in evening asap. Meanwhile You may refer this: http://stackoverflow.com/questions/16111337/declaring-a-priority-queue-in-c-with-a-custom-comparator. (02 Feb '15, 07:49) damn_me3★
 toggle preview community wiki:
Preview

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• link:[text](http://url.com/ "title")
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×2,581
×1,840
×294

question asked: 01 Feb '15, 13:42

question was seen: 1,420 times

last updated: 02 Feb '15, 07:49