I have a class CMyVector
which holds a vector of pointers to CMyClass
objects and I have several "find" functions to find elements according to differente criteria. So for example, I have:
CMyClass* CMyVector::FindByX(int X);
CMyClass* CMyVector::FindByString(const CString& str);
CMyClass* CMyVector::FindBySomeOtherClass(CSomeOtherClass* ptr);
// Other find functions...
At first, they were implemented as loops, traversing the vector, looking for the element that matches X, str, ptr or whatever. So I've created predicates, like this one:
class IsSameX:public unary_function<CMyClass*, bool>
{
int num;
public:
IsSameX(int n):num(n){}
bool operator()(CMyClass* obj) const
{
return (obj != NULL && (obj->X() == num));
}
};
And ended with a bunch of functions which all look like this:
CMyClass* CMyVector::FindByX( int x )
{
CMyVector::iterator it = find_if(vec.begin(), vec.end(), IsSameX(x));
if (it != vec.end())
{
return *it;
}
return NULL;
}
They all look the same, except for the predicate that is called, so I've thought of simplifying more, and created a function like this one:
CMyClass* CMyVector::Find( ThisIsWhatIDontKnow Predicate)
{
CMyVector::iterator it = find_if(vec.begin(), vec.end(), Predicate);
if (it != vec.end())
{
return *it;
}
return NULL;
}
And do:
CMyClass* CMyVector::FindByX( int x )
{
return Find(IsSameX(x));
}
And so on.
So my question is: How should I declare my Find
function so I can pass it my predicates? I've tried several ways, but with no luck so far.