[英]C++ remove_if on a vector of objects
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. 我认为最好的方法是将特定的myobj对象标记为删除,然后在向量上调用myList.remove_if()。 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然后创建一个谓词来检查成员变量是否已设置?
How do I implement the predicate as a part of the vectorList class? 如何将谓词实现为vectorList类的一部分?
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? 这不是
m_bMarkedDelete
的用途吗? You would write the predicate like this: 你会像这样编写谓词:
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: 或者,使用lambdas:
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.
所以你需要一些方法来访问它们。
A predicate is basically a conditional comparison. 谓词基本上是条件比较。 It can be a function or object.
它可以是一个功能或对象。 Here's an example using new C++ lambdas.
这是使用新C ++ lambda的示例。 This code will go through the vector and remove the values equal to 3.
此代码将遍历向量并删除等于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. 编辑:对于指针,假设您有一个矢量或接口,您可以将它们设置为
nullptr
然后使用几乎相同的代码批量删除它们。 In VS2008 you won't have lambdas so make a comparison predicate function or struct instead. 在VS2008中你不会有lambdas所以请改为使用比较谓词函数或结构。
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.