std::unordered_map::erase(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 at pos.
  • 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 to key.
  • 5) β€” Removes all elements with key that compares equivalent to the value x. This overload participates in overload resolution only if Hash::is_transparent and KeyEqual::is_transparent are valid and each denotes a type, and neither iterator nor const_iterator is implicitly convertible from K. This assumes that such Hash is callable with both K and Key type, and that the KeyEqual is transparent, which, together, allows calling this function without constructing an instance of Key.

References and iterators to the erased elements are invalidated. Other iterators and references are not invalidated. 🫢

The iterator pos must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferenceable) cannot be used as a value for pos. 😭 Don’t do it, bestie.

πŸŽ€ 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 (0 or 1).
  • 5) β€” Number of elements removed.

πŸ”₯ Exceptions πŸ”₯

  • 1-3) β€” Throws nothing.
  • 4,5) β€” Any exceptions thrown by the Hash and KeyEqual object.

πŸ¦‹ 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 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).

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