std::unorde...nsert_range(3)

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

πŸ’– std::unordered_map::insert_range πŸ’–

okay bestie, this is the std::unordered_map::insert_range page from the C++ Standard Libary manual β€” the header shortens it to std::unorde...nsert_range(3) because terminal headers are dramatic like that πŸ’…. This is the C++23 member function that inserts stuff from a range, and it's very "only if the key isn't already here" about it.


🌸 NAME 🌸

std::unordered_map::insert_range β€” std::unordered_map::insert_range. That's the name, fr fr.

✨ Synopsis ✨

template< container-compatible-range<value_type> R >  (since C++23)
void insert_range( R&& rg );

It inserts a copy of each element in the range rg if and only if there is no element with key equivalent to the key of that element in *this.

Each iterator in the range rg is dereferenced exactly once. The behavior is undefined if rg overlaps with the container.

🚨 undefined behavior alert: if rg overlaps with the container, the behavior is undefined. Not cute, not safe, don't do it.

If after the operation the new number of elements is greater than old max_load_factor() * bucket_count() a rehashing takes place. If rehashing occurs (due to the insertion), all iterators are invalidated. Otherwise (no rehashing), iterators are not invalidated.


πŸŽ€ Parameters πŸŽ€

  • rg β€” a container compatible range, that is, an input_range whose elements are convertible to T.

πŸ¦‹ Type requirements πŸ¦‹

  • value_type must be EmplaceConstructible into unordered_map from *ranges::begin(rg). Otherwise, the behavior is undefined.

🌈 Return value 🌈

(none) β€” literally none. It returns nothing, bestie.

πŸ”₯ Complexity πŸ”₯

Average case O(N), where N is ranges::distance(rg), worst case O(NΒ·(a.size() + 1)).

πŸ’œ Notes πŸ’œ

  • Feature-test macro __cpp_lib_containers_ranges β€” value 202202L, standard C++23, feature: Ranges-aware construction and insertion.

πŸ“ Example πŸ“

The page says // Run this code, so go run it, bestie:

        #include <iostream>
        #include <unordered_map>
        #include <utility>

        void println(auto, auto const& container)
        {
            for (const auto& [key, value] : container)
                std::cout << '{' << key << ',' << value << '}' << ' ';
            std::cout << '\n';
        }

        int main()
        {
            auto container = std::unordered_map{std::pair{1, 11}, {3, 33}, {2,
       22}, {4, 44}};
            const auto rg = {std::pair{-1, -11}, {3, -33}, {-2, -22}};
        #ifdef __cpp_lib_containers_ranges
            container.insert_range(rg);
        #else
            container.insert(rg.begin(), rg.end());
        #endif
            println("{}", container);
        }

Possible output:

{4,44} {-1,-11} {2,22} {3,33} {-2,-22} {1,11}

πŸ‘‘ See also πŸ‘‘

  • insert β€” inserts elements or nodes (since C++17) (public member function).

mwah πŸ’‹ β€” this page came from http://cppreference.com, version/date 2024.06.10, and its manual name is std::unorde...nsert_range(3). Periodt. 🫢

πŸ“¦ where this page comes from