compare function template

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • antani

    compare function template

    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>::iter ator 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?=

    #2
    Re: compare function template

    On 7 Mar, 12:26, "antani" <antani8...@yah oo.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>::iter ator 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<typena me CMP>
    void f(CMP comp)
    {
    list<intl;
    list<int>::iter ator 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

    Comment

    • p.lepin@ctncorp.com

      #3
      Re: compare function template

      On Mar 7, 1:26 pm, "antani" <antani8...@yah oo.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

      Comment

      • antani

        #4
        Re: compare function template

        template<typena me CMP>
        void f(CMP comp)
        {
        list<intl;
        list<int>::iter ator 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?

        Comment

        • Wayne Shu

          #5
          Re: compare function template

          On 3ÔÂ7ÈÕ, ÏÂÎç9ʱ46·Ö, "antani" <antani8...@yah oo.itwrote:
          template<typena me CMP>
          void f(CMP comp)
          {
          list<intl;
          list<int>::iter ator 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;
          }
          - ÏÔʾÒýÓõÄÎÄ×Ö -

          Comment

          • Wayne Shu

            #6
            Re: compare function template

            On 3ÔÂ7ÈÕ, ÏÂÎç10ʱ19·Ö, "Wayne Shu" <Wayne...@gmail .comwrote:
            On 3ÔÂ7ÈÕ, ÏÂÎç9ʱ46·Ö, "antani" <antani8...@yah oo.itwrote:
            >
            >
            >
            template<typena me CMP>
            void f(CMP comp)
            {
            list<intl;
            list<int>::iter ator 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;
            >
            >
            >
            }
            - ÏÔʾÒýÓõÄÎÄ×Ö -- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
            >
            - ÏÔʾÒýÓõÄÎÄ×Ö -- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
            >
            - ÏÔʾÒýÓõÄÎÄ×Ö -

            Comment

            Working...