std::unorde...nsert_range(3)
π std::unordered_map::insert_range π
okay bestie, this is the std::unordered_map::insert_range page from the C++ Standard Libary manual β the header shortens it to std::unorde...nsert_range(3) because terminal headers are dramatic like that π
. This is the C++23 member function that inserts stuff from a range, and it's very "only if the key isn't already here" about it.
πΈ NAME πΈ
std::unordered_map::insert_range β std::unordered_map::insert_range. That's the name, fr fr.
β¨ 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 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.
π¨ undefined behavior alert: if rg overlaps with the container, the behavior is undefined. Not cute, not safe, don't do it.
If after the operation the new number of elements is greater than old max_load_factor() * bucket_count() a rehashing takes place. If rehashing occurs (due to the insertion), all iterators are invalidated. Otherwise (no rehashing), iterators are not invalidated.
π Parameters π
rgβ a container compatible range, that is, aninput_rangewhose elements are convertible toT.
π¦ Type requirements π¦
value_typemust beEmplaceConstructibleintounordered_mapfrom*ranges::begin(rg). Otherwise, the behavior is undefined.
π Return value π
(none) β literally none. It returns nothing, bestie.
π₯ Complexity π₯
Average case O(N), where N is ranges::distance(rg), worst case O(NΒ·(a.size() + 1)).
π Notes π
- Feature-test macro
__cpp_lib_containers_rangesβ value202202L, standardC++23, feature: Ranges-aware construction and insertion.
π Example π
The page says // Run this code, so go run it, bestie:
#include <iostream>
#include <unordered_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::unordered_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);
}
Possible output:
{4,44} {-1,-11} {2,22} {3,33} {-2,-22} {1,11}
π See also π
insertβ inserts elements or nodes (since C++17) (public member function).
mwah π β this page came from http://cppreference.com, version/date 2024.06.10, and its manual name is std::unorde...nsert_range(3). Periodt. π«Ά
π¦ 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