2

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.

// 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?

sth
  • 222,467
  • 53
  • 283
  • 367
fishfood
  • 4,092
  • 4
  • 28
  • 35

1 Answers1

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.

jrok
  • 54,456
  • 9
  • 109
  • 141
  • thanks. will accept in 10 mins when I'm allowed :) I spent all my time looking up the std::remove documentation, I should've looked closer at vector::erase! – fishfood Apr 30 '13 at 14:02