compare function template

A

antani

I need to implement a function with a argument that is a compare
function.
This compare function must be several for every necessity.
For example , I would like a compare function to analyze element of
list that are even or a compare function to analyze odd element.


example:

void f (compare )
{

list<int> l;
list<int>::iterator it;

for (it=l.begin();l!=l.end();++it)
{
if ( compare(it) )
{
....
}

}

}


I would like specify in my call, what type of compare function the
function f must use.
Exist a template solution for that?
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I need to implement a function with a argument that is a compare
function.
This compare function must be several for every necessity.
For example , I would like a compare function to analyze element of
list that are even or a compare function to analyze odd element.

example:

void f (compare )
{

list<int> l;
list<int>::iterator it;

for (it=l.begin();l!=l.end();++it)
{
if ( compare(it) )
{
....
}

}

}

I would like specify in my call, what type of compare function the
function f must use.
Exist a template solution for that?

I'm not quite sure I understand what you want but you probably want to
use templates here, something like:

template<typename CMP>
void f(CMP comp)
{
list<int> l;
list<int>::iterator it;

for (it=l.begin();l!=l.end();++it)
{
if ( comp(it) )
{
....
}
}
}

This way you can use either a functor or a normal function when
calling f().
 
P

p.lepin

I need to implement a function with a argument that is a
compare function.
This compare function must be several for every
necessity. For example , I would like a compare function
to analyze element of list that are even or a compare
function to analyze odd element.

I almost choked on my coffee when my compiler didn't choke
on the following:

#include <iostream>
#include <ostream>
#include <list>

template < typename T >
bool fredle ( T x ) { return ( * x ) % 2 ; }

template < typename T >
bool bargle ( T x ) { return ! ( ( * x ) % 2 ) ; }

template < typename T >
bool quugle ( T x ) { return ! ( ( * x ) % 3 ) ; }

template < typename T >
bool xyzzle ( T x ) { return quugle ( -- x ) ; }

template < typename T >
void f
(
const T & l ,
bool ( * c ) ( typename T :: const_iterator )
)
{
for
(
typename T :: const_iterator i = l . begin ( ) ;
i != l . end ( ) ; ++ i
)
if ( ( * c ) ( i ) ) std :: cout << ( * i ) << " " ;
std :: cout << std :: endl ;
}

int main ( )
{
std :: list < int > l ;
for ( int i = 0 ; i != 10 ; ++ i )
l . push_back ( i + 1 ) ;
f ( l , & fredle ) ;
f ( l , & bargle ) ;
f ( l , & quugle ) ;
f ( l , & xyzzle ) ;
}

Looks like a disaster waiting to happen to me.
 
A

antani

template<typename CMP>
void f(CMP comp)
{
list<int> l;
list<int>::iterator it;

for (it=l.begin();l!=l.end();++it)
{
if ( comp(it) )
{
....
}
}
}

This way you can use either a functor or a normal function when
calling f().

Can you write a example how declare a CMP comp?
 
W

Wayne Shu

Can you write a example how declare a CMP comp?- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
for example:

#include <iostream>

// the normal function
void foo()
{
std::cout << "foo" << std::endl;
}

// the function object
class bar
{
void operator ()(){
std::cout << "bar" << std::endl;
}
}

template <typename CMP>
void foobar(CMP cmp)
{
cmp();
}

int main()
{
foobar(foo);
foobar(bar());

return 0;
}
 
W

Wayne Shu

for example:

#include <iostream>

// the normal function
void foo()
{
std::cout << "foo" << std::endl;

}

// the function object
class bar
{
sorry I have forgotten to add the access level.
public:
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top