You ask for most “effective”. There is a possibility that you meant “efficient”. Most “effective” is the method that in most case do what you want, clearing that vector.
And that is the clear()
method, because erase()
adds requirements on the items, i.e. it's less universally applicable.
[1]cppreference.com about erase
:
” T must meet the requirements of MoveAssignable.
Probably Mat
is move-assignable or copyable, but if it isn't, then you just can't use erase
.
Method 3, looping over all vector items and using an item-specific way to return each item to a destructible nullstate/emptystate, is clearly the least effective as it only applies to item types with nullstates/emptystates. Note, since it's not included in the example, that you need to adjust the vector size after that.
If instead of effective you really meant efficient, then it all depends on the type Mat
, your compiler's implementation of std::vector
, the compiler options, the environment your program is running in, etc. I.e. then you have no recourse but to measure. A nice consequence of measuring can be that you find that no particular optimization is needed, so then you can most probably just use clear
as the most clear way, which is what's generally most important. ;-)
[1] At (http://en.cppreference.com/w/cpp/container/vector/erase).