C++ remove_if 在对象向量上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7958216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ remove_if on a vector of objects
提问by Jordan
I have a vector (order is important) of objects (lets call them myobj class) where I'm trying to delete multiple objects at a time.
我有一个对象的向量(顺序很重要)(让我们称它们为 myobj 类),我试图一次删除多个对象。
class vectorList
{
vector<*myobj> myList;
};
class myobj
{
char* myName;
int index;
bool m_bMarkedDelete;
}
I was thinking that the best way to do this would be to mark specific myobj objects for deletion and then call myList.remove_if() on the vector. However, I'm not exactly sure how to use predicates and such for this. Should I create a member variable in the object which allows me to say that I want to delete the myobj and then create a predicate which checks to see if the member variable was set?
我认为最好的方法是将特定的 myobj 对象标记为删除,然后在向量上调用 myList.remove_if() 。但是,我不确定如何为此使用谓词等。我应该在对象中创建一个成员变量,它允许我说我想删除 myobj 然后创建一个谓词来检查是否设置了成员变量?
How do I implement the predicate as a part of the vectorList class?
如何将谓词实现为 vectorList 类的一部分?
回答by Benjamin Lindley
Should I create a member variable in the object which allows me to say that I want to delete the myobj and then create a predicate which checks to see if the member variable was set?
我应该在对象中创建一个成员变量,它允许我说我想删除 myobj 然后创建一个谓词来检查是否设置了成员变量?
Haven't you already done that? Isn't that what m_bMarkedDelete
is for? You would write the predicate like this:
你不是已经这样做了吗?不m_bMarkedDelete
就是为了这个吗?你可以这样写谓词:
bool IsMarkedToDelete(const myobj & o)
{
return o.m_bMarkedDelete;
}
Then:
然后:
myList.erase(
std::remove_if(myList.begin(), myList.end(), IsMarkedToDelete),
myList.end());
Or, using lambdas:
或者,使用 lambda 表达式:
myList.erase(
std::remove_if(myList.begin(), myList.end(),
[](const myobj & o) { return o.m_bMarkedDelete; }),
myList.end());
If your class doesn't actually have that member, and you're asking us if it should, then I would say no. What criteria did you use to decide to mark it for deletion? Use that same criteria in your predicate, for example:
如果你的班级实际上没有那个成员,而你问我们是否应该,那么我会说不。您使用什么标准来决定将其标记为删除?在谓词中使用相同的条件,例如:
bool IndexGreaterThanTen(const myobj & o)
{
return o.index > 10;
}
note-- The functions I've written are of course invalid since all your members are private. So you'll need some way to access them.
注意——我写的函数当然是无效的,因为你所有的成员都是私有的。因此,您需要某种方式来访问它们。
回答by AJG85
A predicate is basically a conditional comparison. It can be a function or object. Here's an example using new C++ lambdas. This code will go through the vector and remove the values equal to 3.
谓词基本上是条件比较。它可以是一个函数或对象。这是一个使用新 C++ lambda 的示例。此代码将遍历向量并删除等于 3 的值。
int arg[6] = {1, 2, 3, 3, 3, 5};
std::vector<int> vec(arg, arg+6);
vec.erase(
std::remove_if(
vec.begin(), vec.end(),
[](int i){ return i == 3;}),
vec.end());
Edit:For pointers let's say you had a vector or interfaces you could set them to nullptr
then remove them in a batch with pretty much the same code. In VS2008 you won't have lambdas so make a comparison predicate function or struct instead.
编辑:对于指针,假设您有一个向量或接口,您可以将它们设置为nullptr
然后使用几乎相同的代码批量删除它们。在 VS2008 中,您将没有 lambda,因此请改用比较谓词函数或结构。
bool ShouldDelete(IAbstractBase* i)
{
return i == nullptr;
// you can put whatever you want here like:
// return i->m_bMarkedDelete;
}
std::vector<IAbstractBase*> vec;
vec.erase(
std::remove_if(
vec.begin(), vec.end(),
ShouldDelete),
vec.end());