我的谓词函数有什么问题?(What's wrong with my predicate function?)

我正在尝试使用std :: list的remove_if ”方法。 我想删除“特殊”元素。 这里有一些代码:

Class A { public: void foo(size_t id) { tasks.remove_if(&A::IsEqual(id)); //Here I have an error } private: std::list<Task> tasks; struct IsEqual { IsEqual(const Task& value) : _value(value) {} bool operator() (const size_t id) { return (_value._id == id); } Task _value; }; };

有人可以解释错误在哪里吗?

I'm trying to use "remove_if" method by std::list. I wanna delete the "special" element. Here some code:

Class A { public: void foo(size_t id) { tasks.remove_if(&A::IsEqual(id)); //Here I have an error } private: std::list<Task> tasks; struct IsEqual { IsEqual(const Task& value) : _value(value) {} bool operator() (const size_t id) { return (_value._id == id); } Task _value; }; };

Could someone explain where the mistake is?

最满意答案

您的operator()应该接受Task参数,因为这是tasks元素的类型。

另一种写作方式:

tasks.remove_if([id](const Task& t) { return t._id == id });

Your operator() should take a Task argument, since this is the type of the elements in tasks.

Another way to write it:

tasks.remove_if([id](const Task& t) { return t._id == id });我的谓词函数有什么问题?(What's wrong with my predicate function?)

我正在尝试使用std :: list的remove_if ”方法。 我想删除“特殊”元素。 这里有一些代码:

Class A { public: void foo(size_t id) { tasks.remove_if(&A::IsEqual(id)); //Here I have an error } private: std::list<Task> tasks; struct IsEqual { IsEqual(const Task& value) : _value(value) {} bool operator() (const size_t id) { return (_value._id == id); } Task _value; }; };

有人可以解释错误在哪里吗?

I'm trying to use "remove_if" method by std::list. I wanna delete the "special" element. Here some code:

Class A { public: void foo(size_t id) { tasks.remove_if(&A::IsEqual(id)); //Here I have an error } private: std::list<Task> tasks; struct IsEqual { IsEqual(const Task& value) : _value(value) {} bool operator() (const size_t id) { return (_value._id == id); } Task _value; }; };

Could someone explain where the mistake is?

最满意答案

您的operator()应该接受Task参数,因为这是tasks元素的类型。

另一种写作方式:

tasks.remove_if([id](const Task& t) { return t._id == id });

Your operator() should take a Task argument, since this is the type of the elements in tasks.

Another way to write it:

tasks.remove_if([id](const Task& t) { return t._id == id });