<分区>
如果我试图在另一个类中传递一个函数,我很难理解如何使用 count_if 的第三个参数。当我在类外部创建 bool 函数时似乎没有任何问题,但是当我尝试将类内部的 bool 函数传递给 count_if 的第三个参数时,我收到“函数调用缺少参数列表”错误.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//bool greater(int i); // this works
class testing {
vector<int> vec;
public:
testing(vector<int> v) {
vec = v;
}
bool greater(int i) { // this doesn't work
return i > 0;
}
void add(int i) {
vec.push_back(i);
}
void display() {
for (size_t j = 0; j < vec.size(); j++)
cout << vec[j] << endl;
}
};
int main() {
vector<int> vec;
testing t(vec);
for (int i = 0; i < 5; i++) {
t.add(i);
}
t.display();
//cout << count_if(vec.begin(), vec.end(), greater); // this works
cout << count_if(vec.begin(), vec.end(), t.greater); // this doesn't work
system("pause");
return 0;
}
/*bool greater(int i) { // this works
return i > 0;
}*/