I need to count the number of times an object of an array of pointers has the same name(member variable) as the parameter given to a member function. I have tried different approaches, but none of them worked. My code does not even compile. The error is: "error C2514: 'MyComparator' : class has no constructors" and here is the code of the class I use for the comparision and the function used for counting concurencies.
//definition of the vector
vector<Class1*> files;
...
int Class2::countNames(string name)
{
return count_if(files.begin(), files.end(), bind2nd(MyComparator(),name));
}
...
class MyComparator{
public:
bool operator()(const CFileType*& ob1, const string &str)
{
return ob1->getName()==str;
}
};
I am struggling with this for hours and I want to do it using STL. The catch here is that i have an vector of pointers, if I had a normal vector it would not need a predicate function, in my case I have to give a parameter to it and I think bind2nd() is the right way. Any help would be greatly appreciated!
Under C++11, you could use a lambda expression, which is much easier than the archaic
bind2nd. For example:This prints: