std::flat_m...nsert_range(3)
π 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
rgis dereferenced exactly once β - The behavior is undefined if
rgoverlaps 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, aninput_rangewhose elements are convertible toT. 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms