std::flat_m...nsert_range(3)

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

πŸ’… std::flat_multimap::insert_range

ok so bestie, we're in the C++ Standard Libary manual β€” yes that's how it's spelled on the tin, we don't ask questions β€” and today's main character is std::flat_multimap::insert_range. this page is dated 2024.06.10 and it lives at http://cppreference.com, and honestly? it ate. let's get into it 🫢


✨ NAME ✨

std::flat_multimap::insert_range β€” literally just std::flat_multimap::insert_range, no notes, the name is the name πŸ’…


🌸 Synopsis 🌸

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

So what's the vibe? It inserts a copy of each element in the range rg into underlying containers c β€” and it does it as if by this whole situation right here:

for (const auto& e : rg)
{
    c.keys.insert(c.keys.end(), e.first);
    c.values.insert(c.values.end(), e.second);
}

Then β€” and this is the part where it gets iconic β€” it sorts the range of newly inserted elements with respect to value_comp(), and then merges the resulting sorted range and the sorted range of pre-existing elements into a single sorted range. like, it's giving organized. it's giving tidy. 🌈

Two things you NEED to know, no cap:

  • Each iterator in the range rg is dereferenced exactly once ⭐
  • The behavior is undefined if rg overlaps with the container 😭 so like. don't do that. fr.
Information on iterator invalidation is copied from here.

(that's a direct quote, we're just relaying the message 🧚)


πŸŽ€ Parameters πŸŽ€

  • rg β€” a container compatible range, that is, an input_range whose elements are convertible to T. that's it, that's the whole parameter, she's low maintenance πŸ’–

πŸ’œ Return value πŸ’œ

(none) β€” nada, nothing, zero, the void. she does the work and leaves without a word πŸ‘‘


πŸ”₯ Complexity πŸ”₯

N + M·log(M), where N is size() before the operation and M is ranges::distance(rg). math girlies rise 😀


πŸ¦‹ Notes πŸ¦‹

Since insert_range performs an in-place merge operation, that may allocate memory. heads up, bestie β€” it's not free, keep an eye on your allocator πŸ™ˆ


🌟 Example 🌟

// Run this code β€” as the manpage says, go run it ✨

#include <iostream>
#include <flat_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::flat_multimap{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} {3,-33} {4,44}

look at that sorted little family. the {3,33} and {3,-33} sitting next to each other? that's the multimap of it all, periodt πŸŽ€


πŸ‘‘ See also πŸ‘‘

  • insert β€” inserts elements (public member function) πŸ’…

ok bye bestie, go sort something beautiful πŸ’–

β€” that's the whole page, sourced from http://cppreference.com, dated 2024.06.10, in the C++ Standard Libary manual, std::flat_m...nsert_range(3). mwah 🫢🌸

πŸ“¦ where this page comes from