【发布时间】:2012-03-05 23:35:02
【问题描述】:
我需要计算指针数组的对象与赋予成员函数的参数具有相同名称(成员变量)的次数。我尝试了不同的方法,但都没有奏效。我的代码甚至没有编译。错误是:“error C2514: 'MyComparator' : class has no constructors”,这是我用于比较的类的代码和用于计算并发的函数。
//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;
}
};
我为此苦苦挣扎了好几个小时,我想用 STL 来做。这里的问题是我有一个指针向量,如果我有一个法线向量,它就不需要谓词函数,在我的情况下,我必须给它一个参数,我认为 bind2nd() 是正确的方法。任何帮助将不胜感激!
【问题讨论】:
标签: c++ pointers vector countif bind2nd