The standard provides that std::vector<T>::push_back() has an amortization of O(1) . This means that the extension must be geometric, say, double the storage volume each time it is full.
A simple example: sequentially push_back 32 int in std::vector<int> . You will store them all once, and also make 31 copies if you double the capacity each time you finish. Why 31? Before saving the second item, you copy the 1st; before storing the 3rd, you copy elements 1-2, before storing the 5th, you copy 1-4, etc. So you copy 1 + 2 + 4 + 8 + 16 = 31 times with 32 stores.
Performing a formal analysis shows that you get O(N) stores and copies for elements of N This means amortizing the O(1) complexity over push_back (often only storage without a copy, sometimes storage and a sequence of copies).
Because of this expansion strategy, you will have size() < capacity() most of the time. Search shrink_to_fit and reserve to find out how to manage vector capacity in a finer way.
Note : at a geometric growth rate, any factor in excess of 1 will take place, and there have been some studies that claim that 1.5 gives better performance due to less memory loss (since redistributed memory may overwrite old memory at some point) .
source share