Using function objects in own algorithm

A

Alan

I now know how to compose a function object for some standard
algorithms, e.g., std::sort(). However, I cannot find an explanation
of how to use a function object in my own code of an algorithm. All
the links I find focus on the former. Could anyone explain this to me
and/or point me to a clear explanation?

For example, how could I use the function object instantiated from the
class "less metric1" below?

What I want to do is to tell a function (e.g., do-something()) to do
some math based on a particular object`s attribute (out of several
attributes). It is very similar to how std::sort uses it (on the
surface), but I do not know how sort() uses a function object
internally.

Thanks, Alan

class measurement
{
public:
double metric1, metric2, metric3;
};


class less_metric1 : binary_function<measurement, measurement, bool>
{
public:
return_type operator()(const measurement& a, const measurement&
b)
{
return a.metric1 < b.metric1;
}
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top