how to use a static member function in find_if

  • Thread starter want.to.be.professer
  • Start date
W

want.to.be.professer

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

puzzlecracker

want.to.be.professer said:
#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?
 
J

Jerry Coffin

#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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top