For Programmers: Free Programming Magazines  


Home > Archive > VC STL > March 2005 > using a non static member function as find_if predicate









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author using a non static member function as find_if predicate
AS

2005-03-10, 4:03 pm

I wish to use find if on a vector containing many CTest objects. Im trying
to use the non static member function CTest::sameUpdateType as the
predictate. Ive tried bind2nd but it wont compile. Ive simplified the code
below, and would appreciate if anyone has any suggestions as to how i can
achieve this.

Thanks
Andy

vector<CTest*> myVector;
myVector::iterator iter = find_if(myVector.begin(), myVector.end(),
bind2nd(mem_fun_ref(&CTest::sameUpdateType), UPDATED_APP_NEW));


class CTest
{
public:
CTest (UPDATED_APP_TYPE m_eUpdateType = UPDATED_APP_UNKNOWN)
{
}

bool sameUpdateType(UPDATED_APP_TYPE eUpdateType )
{
return m_eUpdateType == eUpdateType;
}

//enumerated type;
UPDATED_APP_TYPE m_eUpdateType
};




Igor Tandetnik

2005-03-10, 4:03 pm

"AS" <a@aa.com> wrote in message
news:%23lskiAXJFHA.3184@TK2MSFTNGP10.phx.gbl
> vector<CTest*> myVector;
> myVector::iterator iter = find_if(myVector.begin(), myVector.end(),
> bind2nd(mem_fun_ref(&CTest::sameUpdateType), UPDATED_APP_NEW));


Since you have a vector of pointers rather than objects, you want
mem_fun. This program compiles on VC6 and VC7.1:

#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

class CTest
{
public:
CTest (int m_eUpdateType = 0)
{
}
bool sameUpdateType(int eUpdateType )
{
return m_eUpdateType == eUpdateType;
}
int m_eUpdateType;
};

int main()
{
vector<CTest*> myVector;
vector<CTest*>::iterator iter = find_if(myVector.begin(),
myVector.end(),
bind2nd(mem_fun(&CTest::sameUpdateType), 1));
return 0;
}

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com