std::set::insert_range(3)

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

πŸ’– NAME πŸ’–

okay bestie, welcome to the std::set::insert_range(3) page from the C++ Standard Libary manual β€” yep, Libary is exactly how it's spelled up there and I am leaving it alone, no cap. The name is literally std::set::insert_range β€” std::set::insert_range. That's the whole NAME section, and it's already iconic. πŸ’…


✨ Synopsis ✨

This is the glow-up that landed in C++23:

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

okay so what it actually does: 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. No iterators or references are invalidated. Periodt.

⚠️ the behavior is undefined if rg overlaps with the container. That's the dangerous bit, bestie β€” do not do the overlap thing. πŸ™ˆ

🌸 Parameters 🌸

  • rg β€” a container compatible range, that is, an input_range whose elements are convertible to T. That's the whole parameter section and it ate. πŸ’…

πŸ¦‹ Type requirements πŸ¦‹

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

πŸŽ€ Return value πŸŽ€

  • (none)

It returns nothing, bestie. It just does the thing and keeps it moving. 🫢


πŸ”₯ Complexity πŸ”₯

The cost is NΒ·log(a.size() + N), where N is ranges::distance(rg). That's the whole formula β€” don't misplace a parenthesis, iykyk. 🧚


⭐ Notes ⭐

Feature-test macro tea:

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

🌈 Example 🌈

// Run this code β€” yes, that's the label, and here's the code exactly:

// Run this code

#include <iostream>
#include <set>

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

int main()
{
    auto container = std::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);
}

πŸ’œ Output πŸ’œ

-2 -1 1 2 3 4

πŸ‘‘ See also πŸ‘‘

The source prints insert or nodes with (since C++17) and (public member function) attached, so here's the whole See also situation:

  • insert β€” inserts elements
  • or nodes β€” (since C++17) β€” (public member function) β€” the source line is insert or nodes, so that's the exact tea. πŸ“

mwah πŸ’‹ source/version/date, all preserved: http://cppreference.com, 2024.06.10, and std::set::insert_range(3). bye bestie, go insert a range responsibly 🫢

πŸ“¦ where this page comes from