A Vectors in C++ can resize itself when more elements are added. It also allows deletion of elements.
Below is a very basic idea when array becomes full and user wishes to add an item.
1) Create a bigger sized memory on heap memory (for example memory of double size).
2) Copy current memory elements to the new memory.
3) New item is added now as there is bigger memory available now.
4) Delete the old memory.
However the actual library implementation may be more complex. If we create a new double sized array whenever current array becomes full (or it is about to become full), and copy current elements to new double sized array, we get amortized time complexity as O(1). So a particular insert operation may be costly, but overall time complexity remains O(1). Please refer Analysis of Algorithm | Set 5 (Amortized Analysis Introduction) for proof. So time complexity of push_back() is O(1).
// CPP program to illustrate push_back() #include <iostream> #include <vector> using namespace std; int main() { vector<int> myvector{ 1, 2, 3, 4, 5 }; myvector.push_back(6); // Vector becomes 1, 2, 3, 4, 5, 6 for (auto x : myvector) cout << x << " "; } |
Output :
1 2 3 4 5 6
What is the time complexity of erase() in vector()?
Since erasing an element requires moving other elements (to ensure random access), time complexity of erase is O(1).
// CPP program to illustrate // working of erase() function #include <iostream> #include <vector> using namespace std; int main() { vector<int> myvector{ 1, 2, 3, 4, 5 }; auto it = myvector.begin(); myvector.erase(it); // Printing the Vector for (auto x : myvector) cout << x << " "; return 0; } |
Output :
2 3 4 5
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Does overloading work with Inheritance?
- How does Duff's Device work?
- vector::front() and vector::back() in C++ STL
- vector::empty() and vector::size() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::operator= and vector::operator[ ] in C++ STL
- vector::at() and vector::swap() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector::begin() and vector::end() in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- Minimum number of days required to complete the work
- How does "void *" differ in C and C++?
- Does C++ compiler create default constructor when we write our own?
- Write a C program that does not terminate when Ctrl+C is pressed
- When does compiler create default and copy constructors in C++?
- What does buffer flush means in C++ ?
- Does STL priority queue allow duplicate values?
- What does main() return in C and C++?
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

