std:sort predicate function optional arguments

G

Ganesh

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
 
X

Xiaobin Huang

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_;
};
 
J

Juha Nieminen

Xiaobin said:
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?
 
J

James Kanze

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top