I have an std::vector<IRenderable*>
(_pBkBuffer
in the code below). It contains a number of static objects (at the start of the vector) that don't change, followed by a variable number of dynamic objects.
我有一个std :: vector
// erase-remove, but leave the static renderables intact
_pBkBuffer->erase(
std::remove(
_pBkBuffer->begin() + _nStatics, _pBkBuffer->end(), ???
),
_pBkBuffer->end()
);
What can I put at the ??? in order to erase-remove the non-static renderables?
我能把什么?为了擦除 - 删除非静态可渲染物?
I know that the ??? should match all objects in the specified subset.
我知道???应匹配指定子集中的所有对象。
Should I be using erase-remove at all, or should I use another approach?
我应该使用擦除 - 删除,还是应该使用其他方法?
1 个解决方案
#1
4
'Should I be using erase-remove at all
'我应该使用擦除 - 删除
Aparently you already know where the object are, so no. You do this:
显然你已经知道对象在哪里,所以没有。你做这个:
_pBkBuffer->erase( _pBkBuffer->begin() + _nStatics, _pBkBuffer->end() );
or, even better:
或者,甚至更好:
_pkBuffer->resize( _nStatics );
Erase remove idiom would be used if you had them scattered randomly in the vector. What's missing instead of ???
is a value that elements to be removed are compared to. Since you're storing pointers, you'd most likely need to provide a custom predicate (a function pointer, functor, or a lambda) and use remove_if
instead.
如果你在矢量中随机散布它们,将使用擦除删除习语。缺少什么而不是???是要比较要删除的元素的值。由于您存储指针,因此您很可能需要提供自定义谓词(函数指针,仿函数或lambda)并使用remove_if。
#1
4
'Should I be using erase-remove at all
'我应该使用擦除 - 删除
Aparently you already know where the object are, so no. You do this:
显然你已经知道对象在哪里,所以没有。你做这个:
_pBkBuffer->erase( _pBkBuffer->begin() + _nStatics, _pBkBuffer->end() );
or, even better:
或者,甚至更好:
_pkBuffer->resize( _nStatics );
Erase remove idiom would be used if you had them scattered randomly in the vector. What's missing instead of ???
is a value that elements to be removed are compared to. Since you're storing pointers, you'd most likely need to provide a custom predicate (a function pointer, functor, or a lambda) and use remove_if
instead.
如果你在矢量中随机散布它们,将使用擦除删除习语。缺少什么而不是???是要比较要删除的元素的值。由于您存储指针,因此您很可能需要提供自定义谓词(函数指针,仿函数或lambda)并使用remove_if。