Considering the time complexity of vector's push_back() combined with expansion

Every time the vector encounters a power of 2, it needs to be expanded, which involves the old data copy problem, so what is the average complexity of push_back?

The cost of each insertion is Ci, i is the i-th insertion, no matter how big the expansion is, the expansion consumption is not considered, but the

Please add a picture description
Imagine a full binary tree. The number of nodes in the last layer is actually 2. The logN is O(N) level (less than N), and the sum of all the previous nodes is ON level, so the back is 2N. So it can also be understood as 3N, divided by N insertions, is actually O(3n).

Guess you like

Origin blog.csdn.net/myscratch/article/details/129383965