The Standard says:
A deque is a sequence container that supports random access iterators (27.2.7). In addition, it supports constant time insert and erase operations at the beginning or the end; insert and erase in the middle take linear time.
However, it also says in the same Clause:
All of the complexity requirements in this Clause are stated solely in terms of the number of operations on the contained objects. [ Example: The copy constructor of type
vector<vector<int>>
has linear complexity, even though the complexity of copying each containedvector<int>
is itself linear. — end example ]
Doesn't this mean that insertion at the beginning of, say, deque<int>
is allowed to take linear time as long as it doesn't perform more than a constant number of operations on the int
s that are already in the deque and the new int
object being inserted?
For example, suppose that we implement a deque using a "vector of size-K vectors". It seems that once every K times we insert at the beginning, a new size-K vector must be added at the beginning, so all other size-K vectors must be moved. This would mean the time complexity of insertion at the beginning is amortized O(N/K) where N is the total number of elements, but K is constant, so this is just O(N). But it seems that this is allowed by the Standard, because moving a size-K vector doesn't move any of its elements, and the "complexity requirements" are "stated solely in terms of the number of operations" on the contained int
objects.
Does the Standard really allow this? Or should we interpret it as having a stricter requirement, i.e. constant number of operations on the contained objects plus constant additional time?
vector<vector<int>>
but then linear with respect to the elements of the innervector<int>
. If you only consider the number of elements of hte outer vector I'd consider copying the inner vector as constant, though I might be wrong, its already late here – 463035818_is_not_a_number