std::vector::insert is std::vector::insert by definition?
Well no; depends on current power.
From draft N4567, Β§23.3.6.5 / 1 ([vector.modifiers]):
Causes redistribution if the new size is larger than the old capacity .
If the allocated memory capacity in vector is large enough to accommodate new elements, additional allocations for vector not required. So no, then he will not reserve the memory.
If the vector capacity is not large enough, a new block is allocated, the current content is moved / copied, and new elements are inserted. The exact distribution algorithm is not specified, but, as a rule, it will be the same as in the reserve() method.
... or another better approach?
If you are worried about too many selections when inserting elements into vector , then calling the reserve method with the size of the expected number of elements to add will minimize the selection.
vector reserve before / any insertions? Those. Is enough power allocated in a single allocation?
There is no guarantee. How to know the distance between input iterators? Given that the insert method can take an InputIterator (i.e. a one-pass iterator), it cannot calculate the expected size. Can a method calculate the size if iterators are somewhere else (e.g. pointers or RandomAccessIterator )? Yes it can. Will it be? Depends on the implementation and optimizations that are made.
source share