我有Item(一些类)的列表,这个类有 3 个变量price,,name和count。
我想知道如何删除他们价格为的所有项目320。
那这个呢?
std::list<Item> l;
//...
l.remove_if ([] (Item const& i) {
return i.price == 320;
});
请参阅文档:
如果您std::list用作容器,请使用std::list::remove_if; 请参阅@wilx 答案。
如果您不使用std::list,而是使用另一个容器,请使用std::remove_if.
#include <algorithm>
list.erase(std::remove_if(list.begin(), list.end(), [] (Item const& i) {
return i.price == 320;
}), list.end());
以防万一您使用的是 c++ 而不是 c++11 - 它类似于:
bool my_predicate (const Item& value) { return (value.price==320); }
void foo() {
std::list<Item> l;
//...
l.remove_if (my_predicate);
}