std::unordered_map::erase(3)
π std::unordered_map::erase(3) β C++ Standard Libary π
Okay bestie, this is the std::unordered_map::erase(3) page from the C++ Standard Libary manual. Itβs all about removing elements from an unordered_map without wrecking the vibe of the stuff youβre keeping. π
β¨
πΈ NAME πΈ
std::unordered_map::erase β literally std::unordered_map::erase. Thatβs the name, no cap. π
β¨ Synopsis β¨
Here are the overloads, exactly as the page lists them, numbers and all:
iterator erase( iterator pos ); (1)
(since C++11)
iterator erase( const_iterator pos ); (2)
(since C++11)
iterator erase( const_iterator first, const_iterator last ); (3)
(since C++11)
size_type erase( const Key& key ); (4)
(since C++11)
template< class K > (5)
(since C++23)
size_type erase( K&& x );
So the actual job: it removes specified elements from the container. The order of the remaining elements is preserved. (This makes it possible to erase individual elements while iterating through the container.) π¦
1,2)β Removes the element atpos.3)β Removes the elements in the range[first, last), which must be a valid range in*this.4)β Removes the element (if one exists) with the key equivalent tokey.5)β Removes all elements with key that compares equivalent to the valuex. This overload participates in overload resolution only ifHash::is_transparentandKeyEqual::is_transparentare valid and each denotes a type, and neitheriteratornorconst_iteratoris implicitly convertible fromK. This assumes that suchHashis callable with bothKandKeytype, and that theKeyEqualis transparent, which, together, allows calling this function without constructing an instance ofKey.
References and iterators to the erased elements are invalidated. Other iterators and references are not invalidated. π«Ά
The iteratorposmust be valid and dereferenceable. Thus theend()iterator (which is valid, but is not dereferenceable) cannot be used as a value forpos. π Donβt do it, bestie.
π Parameters π
posβ iterator to the element to removefirst, lastβ range of elements to removekeyβ key value of the elements to removexβ a value of any type that can be transparently compared with a key denoting the elements to remove
π Return value π
1-3)β Iterator following the last removed element.4)β Number of elements removed (0or1).5)β Number of elements removed.
π₯ Exceptions π₯
1-3)β Throws nothing.4,5)β Any exceptions thrown by theHashandKeyEqualobject.
π¦ Complexity π¦
Given an instance c of unordered_map: π
1,2)β Average case:constant, worst case:c.size().3)β Average case:std::distance(first, last), worst case:c.size().4)β Average case:c.count(key), worst case:c.size().5)β Average case:c.count(x), worst case:c.size().
π Notes π
Feature-test macro tea, exactly as listed: β
- Feature-test macro:
__cpp_lib_associative_heterogeneous_erasure - Value:
202110L - Std:
(C++23) - Feature: Heterogeneous erasure in associative containers and unordered associative containers; overload
(5)
π Example π
The page says // Run this code right before this exact snippet:
#include <unordered_map>
#include <iostream>
int main()
{
std::unordered_map<int, std::string> c =
{
{1, "one"}, {2, "two"}, {3, "three"},
{4, "four"}, {5, "five"}, {6, "six"}
};
// erase all odd numbers from c
for (auto it = c.begin(); it != c.end();)
{
if (it->first % 2 != 0)
it = c.erase(it);
else
++it;
}
for (auto& p : c)
std::cout << p.second << ' ';
std::cout << '\n';
}
β Possible output β
two four six
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. π
LWG 2059, applied toC++11β behavior as published: βthere was ambiguity for overload (2)β; correct behavior: βadded overload (1)β.LWG 2356, applied toC++11β behavior as published: βthe order of non-equivalent elements that are not erased was not guaranteed to be preservedβ; correct behavior: βrequired to be preservedβ.
π See also π
clearβ clears the contents (public member function).
Footer cute sign-off: source/version http://cppreference.com, date 2024.06.10, page std::unordered_map::erase(3). Byeee bestie, go erase responsibly πππ¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms