std::map::insert_range(3)

๐Ÿ“ฆ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

โœจ std::map::insert_range โœจ

okay bestie, welcome to the std::map::insert_range(3) page from the C++ Standard Libary manual โ€” yes, the header really spells it like that, and honestly? iconic. this is the C++23 glow-up where a std::map gets a whole range inserted into it, and we're gonna read every crumb together. ๐Ÿ’…


๐Ÿ’– NAME ๐Ÿ’–

std::map::insert_range - std::map::insert_range. that's it, that's the NAME line, and the page is serving consistency, no cap. โœจ

๐ŸŒธ Synopsis ๐ŸŒธ

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

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, so do NOT let them touch like that. ๐Ÿ˜ญ no iterators or references are invalidated. periodt. ๐Ÿซถ

๐ŸŽ€ Parameters ๐ŸŽ€

  • rg โ€” a container compatible range, that is, an input_range whose elements are convertible to T. that's the whole parameter, bestie.

๐Ÿ’… Type requirements ๐Ÿ’…

  • value_type must be EmplaceConstructible into map from *ranges::begin(rg). otherwise, the behavior is undefined. do not test fate on this one. ๐Ÿ˜ญ

๐ŸŒˆ Return value ๐ŸŒˆ

(none) โ€” she gives you nothing back, and that's fine. the insertions are the gift. ๐ŸŽ

๐Ÿ”ฅ Complexity ๐Ÿ”ฅ

Nยทlog(a.size() + N), where N is ranges::distance(rg). math ate, fr fr. ๐Ÿงฎโœจ

๐Ÿงš Notes ๐Ÿงš

the feature-test macro table has these columns: Feature-test macro, Value, Std, Feature. and the entry is:

  • __cpp_lib_containers_ranges โ€” value 202202L; std (C++23); feature Ranges-aware construction and insertion. obsessed with a ranges-aware moment. ๐Ÿ’œ

๐Ÿฆ‹ Example ๐Ÿฆ‹

// Run this code

#include <iostream>
#include <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::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);
}

Output:

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

๐Ÿ‘‘ See also ๐Ÿ‘‘

the page's SEE ALSO section lists inserts elements, then insert or nodes with (since C++17) and (public member function) tags. that's the exact tea as printed โ€” iykyk. ๐Ÿต


๐Ÿ’– sourced from http://cppreference.com, dated 2024.06.10 โ€” mwah, bye bestie! ๐Ÿ’…๐ŸŽ€

๐Ÿ“ฆ where this page comes from