std::unorde...imap::erase(3)
okay bestie, this is the std::unordered_multimap::erase(3) page from the C++ Standard Libary manual β yes, the manual name is literally std::unordered_multimap::erase(3), and we're about to make erasing elements make sense. π«Ά
π·οΈ NAME
std::unordered_multimap::erase β the erase member function of std::unordered_multimap. That's the whole name, periodt. π
β¨ SYNOPSIS β¨
the overloads, exactly as written:
iterator erase( iterator pos ); (1)
iterator erase( const_iterator pos ); (2)
iterator erase( const_iterator first, const_iterator last ); (3)
size_type erase( const Key& key ); (4)
template< class K > (5)
size_type erase( K&& x );
(1),(2),(3),(4)are sinceC++11.(5)is sinceC++23.
It removes the specified elements from the container, and the order of the remaining elements is preserved β yes, 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 all elements 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.
Thepositerator must be valid and dereferenceable. Soend()β which is valid, but is not dereferenceable β cannot be used as a value forpos. no cap, don't do it π
πΈ PARAMETERS πΈ
posβ iterator to the element to remove.first,lastβ range of elements to remove.keyβ key value of the elements to remove.xβ 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.(5)β number of elements removed.
π¦ EXCEPTIONS π¦
(1)-(3)β throws nothing.(4),(5)β any exceptions thrown by theHashandKeyEqualobject.
β COMPLEXITY β
Given an instance c of unordered_multimap:
(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 π§
the feature-test macro tea β:
__cpp_lib_associative_heterogeneous_erasureβ value202110L, std(C++23), feature: Heterogeneous erasure in associative and unordered associative containers; overload(5).
π EXAMPLE π
Run this code, bestie:
#include <unordered_map>
#include <iostream>
int main()
{
std::unordered_multimap<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 to:C++11. Behavior as published: there was ambiguity for overload(2). Correct behavior: added overload(1).LWG 2356β applied to:C++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).
that's the whole page, bestie β source: http://cppreference.com, version/date: 2024.06.10, and the manual name again for the vibes: std::unordered_multimap::erase(3). slay. π
β¨
π¦ 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