compare function template

Discussion in 'C++' started by antani, Mar 7, 2007.

  1. antani

    antani Guest

    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?
     
    antani, Mar 7, 2007
    #1
    1. Advertising

  2. On 7 Mar, 12:26, "antani" <> 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öm
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 7, 2007
    #2
    1. Advertising

  3. antani

    Guest

    On Mar 7, 1:26 pm, "antani" <> 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
     
    , Mar 7, 2007
    #3
  4. antani

    antani Guest


    > 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?
     
    antani, Mar 7, 2007
    #4
  5. antani

    Wayne Shu Guest

    On 3ÔÂ7ÈÕ, ÏÂÎç9ʱ46·Ö, "antani" <> 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;
    }
    > - ÏÔʾÒýÓõÄÎÄ×Ö -
     
    Wayne Shu, Mar 7, 2007
    #5
  6. antani

    Wayne Shu Guest

    On 3ÔÂ7ÈÕ, ÏÂÎç10ʱ19·Ö, "Wayne Shu" <> wrote:
    > On 3ÔÂ7ÈÕ, ÏÂÎç9ʱ46·Ö, "antani" <> 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;
    >
    >
    >
    > }
    > > - ÏÔʾÒýÓõÄÎÄ×Ö -- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -
     
    Wayne Shu, Mar 7, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Allan Schwartz
    Replies:
    1
    Views:
    411
    Victor Bazarov
    Aug 9, 2004
  2. Jim West
    Replies:
    3
    Views:
    779
    Jim West
    Oct 7, 2004
  3. Vijai Kalyan
    Replies:
    4
    Views:
    737
    Vijai Kalyan
    Nov 8, 2005
  4. Jeff Newman
    Replies:
    5
    Views:
    347
    Jeff Newman
    Oct 1, 2007
  5. Peng Yu
    Replies:
    3
    Views:
    801
    Thomas J. Gritzan
    Oct 26, 2008
Loading...

Share This Page