Computer Science Canada How to sort in descending order? |
Author: | HazySmoke)345 [ Tue Aug 07, 2007 7:41 pm ] | ||||
Post subject: | How to sort in descending order? | ||||
By default, sort() sorts everthing in ascending order. That's not what I want. First, I tried this:
The output turned out to be: 2 5 2 4 7 1 7 1 6 3 - not very appealing. Then I tried this:
The compiler gaved me an error... testing.cpp:7: error: no matching function for call to `sort(int[10], int*, <unknown type>)' Apparantly it doesn't recognize what 'operator>' is. What am I doing wrong? |
Author: | PaulButler [ Tue Aug 07, 2007 8:39 pm ] | ||||
Post subject: | RE:How to sort in descending order? | ||||
Shouldn't this be:
(or b > a?) |
Author: | HazySmoke)345 [ Tue Aug 07, 2007 9:16 pm ] |
Post subject: | Re: How to sort in descending order? |
Thanks, you're absolutely right. I guess the compare function works differently in C++ than in C. 8 bits. I still have no clue why wouldn't operator> work, though. Doesn't C++ treat it like any other functions? |
Author: | OneOffDriveByPoster [ Thu Aug 09, 2007 9:20 am ] |
Post subject: | Re: How to sort in descending order? |
You can try std::greater<T> in <functional>. HazySmoke)345 @ Tue Aug 07, 2007 9:16 pm wrote: I still have no clue why wouldn't operator> work, though. Doesn't C++ treat it like any other functions? Yes, but you aren't dealing with operator> when you do ( 0 > 0 ). You cannot do operator>(0, 0). |
Author: | PaulButler [ Fri Aug 10, 2007 7:46 am ] |
Post subject: | RE:How to sort in descending order? |
Is there not a way to make an infix operator into a function? In OCaml you just wrap it in brackets (eg., 5 + 3 is the same as (+) 5 3). Maybe C++ has something similar? |
Author: | wtd [ Fri Aug 10, 2007 11:06 am ] | ||
Post subject: | RE:How to sort in descending order? | ||
O'Caml also lacks operator overloading, which adds some complexity to the issue. The way one does this in C++ is to create a function object.
|