std::unordered_set::erase(3)
β¨ std::unordered_set::erase β the manual page bestie π
Okay so like, this is the <std::unordered_set::erase(3)> page from the C++ Standard Library manual, and honestly? It's giving "I can delete things from a hash set and look cute doing it" energy. Let me walk you through it, no cap. π
πΈ NAME πΈ
std::unordered_set::erase β literally the function that removes stuff, and its name is the same as the function, iconic.
π¦ SYNOPSIS π¦
Here's the tea, bestie β the overloads are a whole vibe and you need to know every single one:
iterator erase( iterator pos );
(since C++11)
(until
C++23)
iterator erase( iterator pos )
(since C++23)
requires(!std::same_as<iterator, const_iterator>);
iterator erase( const_iterator pos ); (1) (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 );
π DESCRIPTION π
This function removes specified elements from the container. The order of the remaining elements is preserved β and honestly that's highkey important because it means you can erase individual elements while you're iterating through the container. Main character energy, fr fr. π₯
Let's break down each overload, because every single one matters:
- Overloads 1 and 2 β Removes the element at
pos. Only one overload is provided ifiteratorandconst_iteratorare the same type. (They're two versions of the same vibe.) - Overload 3 β Removes the elements in the range
[first, last), which must be a valid range in*this. Periodt. - Overload 4 β Removes the element (if one exists) with the key equivalent to
key. If it's not there, no biggie, nothing happens. - Overload 5 β Removes all elements with key that compares equivalent to the value
x. But listen β this overload only participates in overload resolution 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. It's giving "optimized and smart," honestly. π§
Big important caveat, don't skip this: References and iterators to the erased elements are invalidated. Other iterators and references are not invalidated.
Also, and I cannot stress this enough: The iteratorposmust be valid and dereferenceable. So theend()iterator (which is valid, but is not dereferenceable) cannot be used as a value forpos. It's literally invalid, bestie. Don't do it. π
π 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 π
- Overloads 1β3 β Iterator following the last removed element.
- Overload 4 β Number of elements removed (
0or1). - Overload 5 β Number of elements removed.
π«Ά EXCEPTIONS π«Ά
- Overloads 1β3 β Throws nothing. Literally nothing. Chill.
- Overloads 4, 5 β Any exceptions thrown by the
HashandKeyEqualobject.
π COMPLEXITY π
Given an instance c of unordered_set:
- Overloads 1, 2 β Average case: constant, worst case:
c.size(). - Overload 3 β Average case:
std::distance(first, last), worst case:c.size(). - Overload 4 β Average case:
c.count(key), worst case:c.size(). - Overload 5 β Average case:
c.count(x), worst case:c.size().
So like, usually fast, but worst case it's the whole set. Manage your expectations. π
π NOTES π
There's a feature-test macro for the heterogeneous erasure thing, and it's giving very specific:
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_associative_heterogeneous_erasure | 202110L | (C++23) | Heterogeneous erasure in associative containers and unordered associative containers; overload (5) |
Anyway, that table is the whole vibe. Moving on. β¨
π§ EXAMPLE π§
// Run this code
#include <unordered_set>
#include <iostream>
int main()
{
std::unordered_set<int> c = {1, 2, 3, 4, 1, 2, 3, 4};
auto print = [&c]
{
std::cout << "c = { ";
for (int n : c)
std::cout << n << ' ';
std::cout << "}\n";
};
print();
std::cout << "Erase all odd numbers:\n";
for (auto it = c.begin(); it != c.end();)
{
if (*it % 2 != 0)
it = c.erase(it);
else
++it;
}
print();
std::cout << "Erase 1, erased count: " << c.erase(1) << '\n';
std::cout << "Erase 2, erased count: " << c.erase(2) << '\n';
std::cout << "Erase 2, erased count: " << c.erase(2) << '\n';
print();
}
This example is so cute β it prints the set, erases all the odd numbers while iterating (the safe way, bestie), then erases specific keys and shows you the counts. Obsessed. π
π DEFECT REPORTS π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. Not me reading the lore of C++ fixes, but okay:
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2059 | C++11 | there was ambiguity for overload (2) | added overload (1) |
| LWG 2356 | C++11 | the order of non-equivalent elements that are not erased was not guaranteed to be preserved | required to be preserved |
So yeah, they fixed the ambiguity and made the preserved order a guarantee. Slay. π
π SEE ALSO π
clearβ clears the contents (public member function). Lowkey does what it says on the tin.
That's the whole manual page, bestie β every overload, every caveat, every complexity note, all preserved. No details lost, just vibes added. π
This page is from cppreference.com, dated 2024.06.10, and it's titled std::unordered_set::erase(3). β¨
π¦ 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