vector::erase()
doesn't reallocate, it only moves contents within the already-allocated capacity and adjusts the size. (Note that EDIT: That's not really relevant to this scenario.)erase()
doesn't throw bad_alloc
, only copy/move/assignment exceptions.
So calling erase()
should be no less efficient than calling rotate()
.
erase()
is probably more efficient, in fact, since rotate()
not only moves the data that in your particular case you want to preserve, but also the data you're about to throw away - but which the semantics of rotate()
dictate must also be preserved (which in turn probably necessitates either temporary storage or one-by-one movement). You can hope the optimiser manages to deal with this, or you can call erase()
.
Of course, the only real way to tell is to implement, measure, and compare.