how to use a static member function in find_if

Discussion in 'C++' started by want.to.be.professer, Aug 7, 2008.

  1. #include <iostream>
    #include <list>
    #include <algorithm>
    #include <functional>
    using namespace std;

    class T
    {
    public:
    static bool IsEqual( int n, int m )
    {
    return m ==n;
    }
    static void print ( int n )
    {
    std::cout << " " << n << std::endl;
    }
    };

    int main()
    {
    T cT;
    int a[10] = {12, 12,23,35, 23,12, 12,23,35, 23};
    int* q = std::find_if(a, a + 10, std::bind1st( (&T::IsEqual),
    35 ) ); // Error!!!!!!!!!!!!!!!!!!!!!!!!!
    for_each( a, a + 10,
    &T::print ); // Work
    Fine
    //std::cout << "-------" << *q << std::endl;
    return 0;
    }

    But How can I use IsEqual function?
     
    want.to.be.professer, Aug 7, 2008
    #1
    1. Advertising

  2. On Aug 6, 10:19 pm, "Daniel T." <> wrote:
    > "want.to.be.professer" <> wrote:
    > > #include <iostream>
    > > #include <list>
    > > #include <algorithm>
    > > #include <functional>
    > > using namespace std;

    >
    > > class T
    > > {
    > > public:
    > >     static bool IsEqual( int n, int m )
    > >     {
    > >         return m ==n;
    > >     }
    > >     static void print ( int n )
    > >     {
    > >         std::cout << "   " << n << std::endl;
    > >     }
    > > };

    >
    > > int main()
    > > {
    > >     T cT;
    > >     int a[10] = {12, 12,23,35, 23,12, 12,23,35, 23};
    > >     int* q = std::find_if(a, a + 10, std::bind1st( (&T::IsEqual),
    > > 35 ) );    // Error!!!!!!!!!!!!!!!!!!!!!!!!!

    >
    > int* q = find_if( a, a + 10, bind1st( ptr_fun( &T::IsEqual ) ), 35 );
    >
    > http://www.sgi.com/tech/stl/ptr_fun.html
    >
    > >     for_each( a, a + 10,
    > > &T::print );                                                   // Work
    > > Fine
    > >     //std::cout << "-------" << *q << std::endl;
    > >     return 0;
    > > }

    >
    > > But How can I use IsEqual function?

    >
    >


    why do you need ptr_fun here?
     
    puzzlecracker, Aug 7, 2008
    #2
    1. Advertising

  3. want.to.be.professer

    Jerry Coffin Guest

    In article <f231acea-ab17-43cc-adf9-c962d6669404
    @b2g2000prf.googlegroups.com>, says...
    > #include <iostream>
    > #include <list>
    > #include <algorithm>
    > #include <functional>
    > using namespace std;
    >
    > class T
    > {
    > public:
    > static bool IsEqual( int n, int m )
    > {
    > return m ==n;
    > }
    > static void print ( int n )
    > {
    > std::cout << " " << n << std::endl;
    > }
    > };
    >
    > int main()
    > {
    > T cT;
    > int a[10] = {12, 12,23,35, 23,12, 12,23,35, 23};
    > int* q = std::find_if(a, a + 10, std::bind1st( (&T::IsEqual),
    > 35 ) ); // Error!!!!!!!!!!!!!!!!!!!!!!!!!


    Better question: Why would you use find_if at all? The reason to use
    find_if is that you're NOT just looking for a specific value. In this
    case, you're just creating a complicated imitation of std::find:

    int *q = std::find(a, a+10, 35);

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Aug 9, 2008
    #3
    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. 0to60
    Replies:
    4
    Views:
    529
    jeffc
    Nov 21, 2003
  2. John Black
    Replies:
    1
    Views:
    396
    Daniel T.
    May 31, 2004
  3. aling
    Replies:
    6
    Views:
    492
    Xiaobin.Huang
    Oct 30, 2005
  4. Assertor
    Replies:
    1
    Views:
    613
    Victor Bazarov
    Mar 9, 2006
  5. dolphin
    Replies:
    3
    Views:
    1,362
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page