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;
} 1 1416
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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<>()....
|
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...
|
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...
|
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...
|
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...
|
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...);...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |