473,379 Members | 1,191 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,379 software developers and data experts.

Using function objects in own algorithm

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

Jun 18 '07 #1
1 1414
On 2007-06-18 18:16, Alan wrote:
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;
}
Using templates:

#include <iostream>
#include <functional>

template<class Comp>
int doSomething(int a, int b)
{
Comp C;
if (C(a,b))
return a;
else
return b;
}

int main()
{
int a = 5;
int b = 6;

std::cout << doSomething<std::less<int(a, b)
<< std::endl;
std::cout << doSomething<std::greater<int(a, b)
<< std::endl;

}

What you do is to create an instance of the functor and use the
()-operator in the function.

--
Erik Wikström
Jun 18 '07 #2

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

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
5
by: Dorwin C. Black | last post by:
Do any gurus out there know what the source code for the STL binary search algorithm "lower_bound" would look like? I want to make a slight modification called "lower_bound_if" which would take a...
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...
3
by: Dave | last post by:
Hello all, When working with the STL, under what circumstances may I use a function object that modifies its internal state from one call to the next? I know this can be done with for_each<>()....
40
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
5
by: Daniel Rudy | last post by:
Hello Group, I've looked at this backwards and forwards and I can't seem to find the problem here. What happens is I have a multidimensional array of char that has 3 dimensions with a...
13
by: Sarath | last post by:
What's the advantage of using for_each than the normal iteration using for loop? Is there any 'tweak' to use stream objects as 'Function' parameter of for_each loop. Seems copy function can do...
3
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...);...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.