Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to use a static member function in find_if

Reply
Thread Tools

how to use a static member function in find_if

 
 
want.to.be.professer
Guest
Posts: n/a
 
      08-07-2008
#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:rint ); // Work
Fine
//std::cout << "-------" << *q << std::endl;
return 0;
}

But How can I use IsEqual function?
 
Reply With Quote
 
 
 
 
puzzlecracker
Guest
Posts: n/a
 
      08-07-2008
On Aug 6, 10:19*pm, "Daniel T." <(E-Mail Removed)> wrote:
> "want.to.be.professer" <(E-Mail Removed)> 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:rint ); * * * * * * * * * * * * * * * * * * * * * * * * * // Work
> > Fine
> > * * //std::cout << "-------" << *q << std::endl;
> > * * return 0;
> > }

>
> > But How can I use IsEqual function?

>
>


why do you need ptr_fun here?
 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      08-09-2008
In article <f231acea-ab17-43cc-adf9-c962d6669404
@b2g2000prf.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
How do you call a regular member function from a static member function? aling C++ 6 10-30-2005 04:38 AM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM



Advertisments