std::multis...insert_range(3)

📦 version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ NAME ✨

Okay bestie, this is std::multiset::insert_range from the C++ Standard Libary man pages, section 3 — the header spells it std::multis...insert_range(3), and it’s giving C++23 range-insertion main character energy. 💅

std::multiset::insert_range — std::multiset::insert_range

✨ 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.

Each iterator in the range rg is dereferenced exactly once. The behavior is undefined if rg overlaps with the container — no cap, that’s the rule. 🫶

No iterators or references are invalidated. Nobody gets invalidated, periodt. ✨

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

✨ Return value ✨

(none) — it’s giving “I do the thing, I don’t hand you a result” energy. 🎀

✨ Complexity ✨

N·log(a.size() + N), where N is ranges::distance(rg).

✨ Notes ✨

Feature-test macro check, bestie:

  • __cpp_lib_containers_ranges — value 202202L, standard (C++23), feature: Ranges-aware construction and insertion.

✨ Example ✨

// 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::multiset{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 3 4

✨ See also ✨

The source’s SEE ALSO bit is a lil scrambled, so here’s the exact reference text so nothing gets lost:

                 inserts elements
          insert or nodes
                 (since C++17)
                 (public member function)
  • insert — inserts elements, and it’s a public member function.
  • The or nodes leftover comes with (since C++17) and (public member function) — that’s literally what the page shows, and I’m not inventing extra references, bestie. 🫶

💖 Source/version/date sign-off: cppreference.com, 2024.06.10 — that’s the footer, and the header was std::multis...insert_range(3). Periodt. 🎀

📦 where this page comes from