std::unorde...nsert_range(3)

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

πŸ¦‹ std::unorde...nsert_range(3) β€” C++ Standard Libary πŸ¦‹

🌸 NAME 🌸

std::unordered_set::insert_range β€” std::unordered_set::insert_range

ngl, the NAME section is literally the same name twice, and honestly? main character energy πŸ’….


✨ Synopsis ✨

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

okay so this is a C++23 moment, fr fr. 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. No dupes allowed, periodt.

Each iterator in the range rg is dereferenced exactly once. The behavior is undefined if rg overlaps with the container. not me risking undefined behavior, please don't do that πŸ™ˆ

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. vibe check: rehash = iterator chaos πŸ”₯


🌟 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_set from *ranges::begin(rg). Otherwise, the behavior is undefined.

πŸŽ€ Return value πŸŽ€

  • (none)

literally returns nothing, it's giving "I did the work, you're welcome" πŸ’…


⭐ Complexity ⭐

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

worst case is highkey rough, but we move 🫢


πŸ”₯ Notes πŸ”₯

the feature-test macro table, but make it cute (I'm obsessed):

  • Feature-test macro: __cpp_lib_containers_ranges
  • Value: 202202L
  • Std: (C++23)
  • Feature: Ranges-aware construction and insertion

🌈 Example 🌈

Run this code, bestie β€” this example ate:

// Run this code

#include <iostream>
#include <unordered_set>

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

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

πŸ“ Possible output πŸ“

4 -1 2 3 -2 1

possible output, bestie πŸ‘€


πŸ‘€ See also πŸ‘€

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

xoxo, your C++ bestie πŸ’– β€” from http://cppreference.com, version/date 2024.06.10, page std::unorde...nsert_range(3).

πŸ“¦ where this page comes from