我有一个Item列表(某个类),这个类有3个变量price、name和count。
我想知道如何删除所有的项目,他们的价格是320。
发布于 2013-02-02 01:38:29
那这个呢?
std::list<Item> l;
//...
l.remove_if ([] (Item const& i) {
return i.price == 320;
});请参阅文档:
发布于 2013-02-02 01:34:25
如果使用std::list作为容器,请使用std::list::remove_if;请参阅@wilx answer。
如果您不使用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());发布于 2013-02-02 02:01:18
以防你使用的是c++而不是c++11 -它类似于:
bool my_predicate (const Item& value) { return (value.price==320); }
void foo() {
std::list<Item> l;
//...
l.remove_if (my_predicate);
}https://stackoverflow.com/questions/14651798
复制相似问题