By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

compare function template

P: n/a
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<intl;
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?

Mar 7 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 7 Mar, 12:26, "antani" <antani8...@yahoo.itwrote:
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<intl;
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<intl;
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().

--
Erik Wikstr鰉

Mar 7 '07 #2

P: n/a
On Mar 7, 1:26 pm, "antani" <antani8...@yahoo.itwrote:
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.

--
Pavel Lepin

Mar 7 '07 #3

P: n/a
template<typename CMP>
void f(CMP comp)
{
list<intl;
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?

Mar 7 '07 #4

P: n/a
On 3月7日, 下午9时46分, "antani" <antani8...@yahoo.itwrote:
template<typename CMP>
void f(CMP comp)
{
list<intl;
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?- 隐藏被引用文字 -
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;
}
- 显示引用的文字 -

Mar 7 '07 #5

P: n/a
On 3月7日, 下午10时19分, "Wayne Shu" <Wayne...@gmail.comwrote:
On 3月7日, 下午9时46分, "antani" <antani8...@yahoo.itwrote:
template<typename CMP>
void f(CMP comp)
{
list<intl;
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?- 隐藏被引用文字 -

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:
void operator ()(){
std::cout << "bar" << std::endl;
}

}

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

}

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

return 0;

}
- 显示引用的文字 -- 隐藏被引用文字 -

- 显示引用的文字 -- 隐藏被引用文字 -

- 显示引用的文字 -

Mar 7 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.