Lets say I was inserting p new elements at the 'i' th position in an std::vector<mytype>
of size 'n'.
Since items in std::vector
are guaranteed to use contiguous storage locations for their elements, it seems like this would take me 4 steps to do the above:
1) Possible reallocation of the vector if we are out of space, basically doubling its size. But that is a constant time operation (albeit a very large one).
2) Next there is a memcpy of elements from index 0 through i-1 from the old vector into the new one.
3) Then you copy 'p' new items being inserted at ith index.
4) Then another memcpy for all items from i+1 through n indexes from the old vector to the new vector.
Aren't all the above constant time operations? Then shouldn't insertion itself be a constant time operation? Why then is std::vector::insert
linear on the number of elements inserted (copy/move construction) plus the number of elements after position (moving)?