Understanding the time complexity of the STL vector from this diagram

I came across a thas site which says that the insert at the end of the STL vector can be either O (1) or O (n). I believe that the insert at the end should be O (1) for the vector. Can someone clarify this and tell me that the author means O (n). The author claims to insert the STL vector on the back panel Back: O(1) or O(n). Which of them?

+4
source share
3 answers

Complexity must be amortized constant.

This means that not every insert necessarily takes the same period of time, but in the long run it averages the value of the constant regardless of the size of the collection.

, , , . "" , , , .

, , . , , ., , , . , .

. -, , , , , , () . -, , , , , . , ( , ).

+10

O (1), , THEN insert. , : .

+5

, , , (, , reserve):

std :: vector has a constant time (O (1)) back if redistribution is not required (use reverse / capacity to allocate / check). When redistribution is required, all elements are copied (or moved, if possible) to the required memory location. It is guaranteed that the back insert is depreciated constant, which means: "if we perform a large number of back inserts, the average time for the back insert is constant."

0
source

Source: https://habr.com/ru/post/1529871/


All Articles