How to Erase Elements from std::map in C++
To erase elements from a
std::map in C++, use the erase() method. You can erase by key, by iterator, or by a range of iterators, which removes the specified elements from the map.Syntax
The erase() method in std::map has three common forms:
map.erase(key);- removes the element with the given key.map.erase(iterator);- removes the element at the given iterator position.map.erase(iterator_first, iterator_last);- removes all elements in the range [iterator_first, iterator_last).
Each form modifies the map by removing elements and adjusts its size accordingly.
cpp
std::map<KeyType, ValueType> map; // Erase by key map.erase(key); // Erase by iterator auto it = map.find(key); if (it != map.end()) { map.erase(it); } // Erase by range map.erase(map.begin(), map.find(some_key));
Example
This example shows how to erase elements from a std::map by key, by iterator, and by range.
cpp
#include <iostream> #include <map> int main() { std::map<int, std::string> myMap = { {1, "apple"}, {2, "banana"}, {3, "cherry"}, {4, "date"}, {5, "elderberry"} }; // Erase by key myMap.erase(2); // removes key 2 // Erase by iterator auto it = myMap.find(4); if (it != myMap.end()) { myMap.erase(it); // removes key 4 } // Erase by range (remove keys less than 4) myMap.erase(myMap.begin(), myMap.find(4)); // Print remaining elements for (const auto& pair : myMap) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; }
Output
5: elderberry
Common Pitfalls
Common mistakes when erasing from a std::map include:
- Trying to erase a key that does not exist — this does nothing but is safe.
- Using an invalid iterator after erasing an element — iterators to erased elements become invalid.
- Erasing elements while iterating without updating the iterator properly, which can cause crashes or undefined behavior.
Always update your iterator correctly when erasing inside a loop.
cpp
#include <map> // Wrong way: erasing inside loop without updating iterator for (auto it = myMap.begin(); it != myMap.end(); ++it) { if (it->first % 2 == 0) { myMap.erase(it); // invalidates iterator, causes crash } } // Right way: update iterator after erase for (auto it = myMap.begin(); it != myMap.end(); ) { if (it->first % 2 == 0) { it = myMap.erase(it); // erase returns next valid iterator } else { ++it; } }
Quick Reference
Summary of std::map erase methods:
| Erase Method | Description | Returns |
|---|---|---|
erase(key) | Removes element with given key | Number of elements removed (0 or 1) |
erase(iterator) | Removes element at iterator position | Iterator following the removed element |
erase(iterator_first, iterator_last) | Removes elements in range [first, last) | Iterator following the last removed element |
Key Takeaways
Use
map.erase(key) to remove an element by its key safely.When erasing by iterator inside loops, update the iterator with the return value of
erase() to avoid invalidation.Erasing a non-existent key does nothing and is safe.
You can erase multiple elements at once using the range form
erase(iterator_first, iterator_last).Always check iterators before erasing to avoid undefined behavior.