C++ .erase()
In C++, the .erase() function removes a single element or a range of elements from a vector. When it removes an element, the size of the vector decreases and the elements after the deleted one are shifted to fill the gap. To remove a range, iterators such as .begin() and .end() are used to specify the start and end positions of the elements to erase.
Syntax
vector.erase(position); // Removing an element
vector.erase(starting_position, ending_position); // Removing multiple elements
Parameters:
position: An iterator pointing to the position of the element to be removed.starting_position: An iterator indicating the beginning of the range of elements to be removed.ending_position: An iterator indicating the end of the range of elements to be removed.
Return value:
The .erase() function doesn’t return any value.
Example 1: Removing an Element Using .erase()
In this example, the .erase() function removes a single element from a vector:
#include <iostream>#include <vector>int main(){// Declare a vectorstd::vector<int> room;// Add valuesfor(int i=1; i<=10; i++) room.push_back(i);// Remove the 3rd elementroom.erase(room.begin()+2);// Print the remaining elements in the vectorstd::cout << "Rooms available:";for (unsigned i=0; i<room.size(); ++i)std::cout << ' ' << room[i];std::cout << '\n';return 0;}
The output for this code is:
Rooms available: 1 2 4 5 6 7 8 9 10
Example 2: Removing Multiple Elements Using .erase()
This example uses .erase() to remove a range of elements from a vector:
#include <iostream>#include <vector>int main(){// Declare a vectorstd::vector<int> room;// Add valuesfor(int i=1; i<=10; i++) room.push_back(i);// Remove the last 3 elementsroom.erase(room.begin()+7, room.end());// Print the remaining elements in the vectorstd::cout << "Rooms available:";for (unsigned i=0; i<room.size(); ++i)std::cout << ' ' << room[i];std::cout << '\n';return 0;}
The output for this code is:
Rooms available: 1 2 3 4 5 6 7
Codebyte Example: Using .erase() with a Map
This codebyte example uses .erase() to remove a key-value pair from a map:
Here, the key-value pair in which the key is “Bob” will be removed.
Frequently Asked Questions
1. Can I use .erase() inside a loop?
Yes, you can use .erase() inside a loop, but with caution. When erasing while iterating, always update the iterator correctly to avoid invalidation issues.
2. What happens if I try to erase a non-existing key from a map using .erase()?
Nothing — no error will occur if you try to erase a non-existing key from a map using .erase(). The size remains unchanged.
3. How efficient is .erase()?
The efficiency of .erase() depends on what it is applied on:
- Vector/String: Linear time O(n) since elements may need to shift.
- Map/Set: Logarithmic time O(log n) due to tree structure.
- Unordered map/Unordered set: Average constant time O(1).
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours