一个
类
或者结构
重载小括号
运算符 使其看起来像一个函数
注意 vector里面存的是非指针 重载的小括号的参数也要是非指针 vector里存的变量类型要和小括号的参数类型一致
struct FindRv
{
SnakeBody body;
FindRv(SnakeBody& body) {
this->body = body;
}
bool operator()(SnakeBody& sb) {
return body == sb;
}
};
struct FindPv
{
SnakeBody body;
FindPv(SnakeBody body) {
this->body = body;
}
bool operator()(SnakeBody* sb) {
return body == sb;
}
};
int main()
{
std::vector<SnakeBody> rv;
std::vector<SnakeBody*> pv;
rv.push_back(SnakeBody(1, 2, 1));
pv.push_back(new SnakeBody(1, 1, 1));
SnakeBody tsb(1,1,2);
std::find_if(rv.begin(), rv.end(), FindRv(tsb));
std::find_if(pv.begin(), pv.end(), FindPv(tsb));
}