std::set::insert_range(3)
π NAME π
okay bestie, welcome to the std::set::insert_range(3) page from the C++ Standard Libary manual β yep, Libary is exactly how it's spelled up there and I am leaving it alone, no cap. The name is literally std::set::insert_range β std::set::insert_range. That's the whole NAME section, and it's already iconic. π
β¨ Synopsis β¨
This is the glow-up that landed in C++23:
template< container-compatible-range<value_type> R > (since C++23)
void insert_range( R&& rg );
okay so what it actually does: 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. No iterators or references are invalidated. Periodt.
β οΈ the behavior is undefined if rg overlaps with the container. That's the dangerous bit, bestie β do not do the overlap thing. π
πΈ Parameters πΈ
rgβ a container compatible range, that is, aninput_rangewhose elements are convertible toT. That's the whole parameter section and it ate. π
π¦ Type requirements π¦
value_typemust beEmplaceConstructibleintosetfrom*ranges::begin(rg). Otherwise, the behavior is undefined. π
π Return value π
(none)
It returns nothing, bestie. It just does the thing and keeps it moving. π«Ά
π₯ Complexity π₯
The cost is NΒ·log(a.size() + N), where N is ranges::distance(rg). That's the whole formula β don't misplace a parenthesis, iykyk. π§
β Notes β
Feature-test macro tea:
- Feature-test macro:
__cpp_lib_containers_ranges - Value:
202202L - Std:
(C++23) - Feature: Ranges-aware construction and insertion
π Example π
// Run this code β yes, that's the label, and here's the code exactly:
// Run this code
#include <iostream>
#include <set>
void println(auto, auto const& container)
{
for (const auto& elem : container)
std::cout << elem << ' ';
std::cout << '\n';
}
int main()
{
auto container = std::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);
}
π Output π
-2 -1 1 2 3 4
π See also π
The source prints insert or nodes with (since C++17) and (public member function) attached, so here's the whole See also situation:
insertβ inserts elementsor nodesβ(since C++17)β(public member function)β the source line isinsert or nodes, so that's the exact tea. π
mwah π source/version/date, all preserved: http://cppreference.com, 2024.06.10, and std::set::insert_range(3). bye bestie, go insert a range responsibly π«Ά
π¦ 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