473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

std:sort predicate function optional arguments

hi !

Can the predicate function used in std::sort take optional
arguments ? For instance. I have a class Point. I create a vector of
this and then want to compare the slopes of these points with respect
to another point of the same class (Graham's convex hull algorithm
anyone ?)

ganesh
Sep 29 '08 #1
5 3160
On 9ÔÂ29ÈÕ, ÏÂÎç6ʱ01·Ö, Ganesh <ganesh.i...@gmail.comwrote:
hi !

Can the predicate function used in std::sort take optional
arguments ? For instance. I have a class Point. I create a vector of
this and then want to compare the slopes of these points with respect
to another point of the same class (Graham's convex hull algorithm
anyone ?)

ganesh
you can provide your own predicate.

class cmp {
public:
cmp(...) {
// init thePoint_
}
bool operator() (point const& lh, point const& rh) {
// ...
}
private:
point thePoint_;
};
Sep 29 '08 #2
Xiaobin Huang wrote:
you can provide your own predicate.

class cmp {
public:
cmp(...) {
// init thePoint_
}
bool operator() (point const& lh, point const& rh) {
// ...
}
private:
point thePoint_;
};
Nitpicking, but isn't that called a comparator? If is it *also* a
predicate?
Sep 29 '08 #3
On 2008-09-29 13:45:24 -0400, Juha Nieminen <no****@thanks.invalidsaid:
Xiaobin Huang wrote:
>you can provide your own predicate.

class cmp {
public:
cmp(...) {
// init thePoint_
}
bool operator() (point const& lh, point const& rh) {
// ...
}
private:
point thePoint_;
};

Nitpicking, but isn't that called a comparator? If is it *also* a
predicate?
There is a version of std::sort that takes a predicate. It doesn't care
whether you call it a comparator, a functoid, or fred. But when you're
talking about std::sort, if you refer to a predicate by some other
name, you run the risk of confusing your listener.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 29 '08 #4
On Sep 29, 9:51 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-09-29 13:45:24 -0400, Juha Nieminen <nos...@thanks.invalidsaid:
Xiaobin Huang wrote:
you can provide your own predicate.
class cmp {
public:
cmp(...) {
// init thePoint_
}
bool operator() (point const& lh, point const& rh) {
// ...
}
private:
point thePoint_;
};
Nitpicking, but isn't that called a comparator? If is it
*also* a predicate?
There is a version of std::sort that takes a predicate. It
doesn't care whether you call it a comparator, a functoid, or
fred. But when you're talking about std::sort, if you refer to
a predicate by some other name, you run the risk of confusing
your listener.
While it's obviously a predicate; any functional object which
returns bool is a predicate. The standard never uses the word
predicate for it, however, and calls it Compare. It also
imposes a number of additional constraints on it which don't
apply to predicates in general. In fact, it even says that "A
sequence is sorted with respect to a comparator comp if [...]",
using the very same word as Juha. So while it is a predicate,
I'd consider "comparator" a more precise word. (All comparators
are predicates, but not all predicates are comparators. And
here, we need a predicate which is a comparator.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 30 '08 #5
On 2008-09-30 05:16:36 -0400, James Kanze <ja*********@gmail.comsaid:
On Sep 29, 9:51 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2008-09-29 13:45:24 -0400, Juha Nieminen <nos...@thanks.invalidsaid:
>>Xiaobin Huang wrote:
you can provide your own predicate.
>>>class cmp {
public:
cmp(...) {
// init thePoint_
}
bool operator() (point const& lh, point const& rh) {
// ...
}
private:
point thePoint_;
};
>>Nitpicking, but isn't that called a comparator? If is it
*also* a predicate?
>There is a version of std::sort that takes a predicate. It
doesn't care whether you call it a comparator, a functoid, or
fred. But when you're talking about std::sort, if you refer to
a predicate by some other name, you run the risk of confusing
your listener.

While it's obviously a predicate; any functional object which
returns bool is a predicate. The standard never uses the word
predicate for it, however, and calls it Compare. It also
imposes a number of additional constraints on it which don't
apply to predicates in general. In fact, it even says that "A
sequence is sorted with respect to a comparator comp if [...]",
using the very same word as Juha. So while it is a predicate,
I'd consider "comparator" a more precise word. (All comparators
are predicates, but not all predicates are comparators. And
here, we need a predicate which is a comparator.)
You're right, of course. Sorting and partitioning rely on comparators
(which take two arguments), and most other algorithms rely on
predicates (which take one argument). I should know better than to post
while I'm in the middle of post-meeting editing.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 30 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
6
by: alexhong2001 | last post by:
Does "std::sort" work only with sequence containers, not associative containers at all? Among sequential containers, can it be used with "list", "queue" and other sequence containers besides...
9
by: JasBascom | last post by:
say i had 97456 and 23456 is there already a sort function to check which one begins with the smaller number rearrange it. in this case the bottom number should clearly be rearranged to the...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
15
by: Peter Olcott | last post by:
Does anyone know how to do this?
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
1
by: mscava | last post by:
How can I make this construction valid? It still gives me error about no matching function std::sort(...). I made a little search and wrong thing is probably the predicate... template <typename...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.