Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > compare function template

Reply
Thread Tools

compare function template

 
 
antani
Guest
Posts: n/a
 
      03-07-2007
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?

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      03-07-2007
On 7 Mar, 12:26, "antani" <antani8...@yahoo.it> wrote:
> 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().

--
Erik Wikstr鰉

 
Reply With Quote
 
 
 
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 1:26 pm, "antani" <antani8...@yahoo.it> wrote:
> 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

 
Reply With Quote
 
antani
Guest
Posts: n/a
 
      03-07-2007

> 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?

 
Reply With Quote
 
Wayne Shu
Guest
Posts: n/a
 
      03-07-2007
On 3月7日, 下午9时46分, "antani" <antani8...@yahoo.it> wrote:
> > 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?- 隐藏被引用文字 -
>

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;
}
> - 显示引用的文字 -



 
Reply With Quote
 
Wayne Shu
Guest
Posts: n/a
 
      03-07-2007
On 3月7日, 下午10时19分, "Wayne Shu" <Wayne...@gmail.com> wrote:
> On 3月7日, 下午9时46分, "antani" <antani8...@yahoo.it> wrote:
>
>
>
> > > 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?- 隐藏被引用文字 -

>
> 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;
>
>
>
> }
> > - 显示引用的文字 -- 隐藏被引用文字 -

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Template function as argument to another template function Jim West C++ 3 10-07-2004 07:21 PM
function non-template versus fully specialized function template Robert Allan Schwartz C++ 1 08-09-2004 03:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57