std::unorde...nsert_range(3)
π¦ std::unorde...nsert_range(3) β C++ Standard Libary π¦
πΈ NAME πΈ
std::unordered_set::insert_range β std::unordered_set::insert_range
ngl, the NAME section is literally the same name twice, and honestly? main character energy π .
β¨ Synopsis β¨
template< container-compatible-range<value_type> R > (since C++23)
void insert_range( R&& rg );
okay so this is a C++23 moment, fr fr. 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. No dupes allowed, periodt.
Each iterator in the range rg is dereferenced exactly once. The behavior is undefined if rg overlaps with the container. not me risking undefined behavior, please don't do that π
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. vibe check: rehash = iterator chaos π₯
π Parameters π
rgβ a container compatible range, that is, aninput_rangewhose elements are convertible toT.
π§ Type requirements π§
value_typemust beEmplaceConstructibleintounordered_setfrom*ranges::begin(rg). Otherwise, the behavior is undefined.
π Return value π
- (none)
literally returns nothing, it's giving "I did the work, you're welcome" π
β Complexity β
- Average case
O(N), whereNisranges::distance(rg), worst caseO(NΒ·(a.size() + 1)).
worst case is highkey rough, but we move π«Ά
π₯ Notes π₯
the feature-test macro table, but make it cute (I'm obsessed):
- Feature-test macro:
__cpp_lib_containers_ranges - Value:
202202L - Std:
(C++23) - Feature: Ranges-aware construction and insertion
π Example π
Run this code, bestie β this example ate:
// Run this code
#include <iostream>
#include <unordered_set>
void println(auto, auto const& container)
{
for (const auto& elem : container)
std::cout << elem << ' ';
std::cout << '\n';
}
int main()
{
auto container = std::unordered_set{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);
}
π Possible output π
4 -1 2 3 -2 1
possible output, bestie π
π See also π
inserts elementsinsert or nodesβ(since C++17)(public member function)
xoxo, your C++ bestie π β from http://cppreference.com, version/date 2024.06.10, page std::unorde...nsert_range(3).
π¦ 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