std::vector::assign_range(3)
π std::vector::assign_range(3) π
okay so we're opening the std::vector::assign_range(3) page from the C++ Standard Libary manual, and honestly? this one is giving βreplace the whole containerβs vibe with a rangeβ energy. letβs go β¨
β¨ NAME β¨
std::vector::assign_range - std::vector::assign_range
π¦ Synopsis π¦
template< container-compatible-range<T> R > (since C++23)
constexpr void assign_range( R&& rg );
πΈ Description πΈ
So what it does is: it replaces the elements in the container with a copy of each element in rg. cute, simple, effective π
All iterators β including the end() iterator β and references to the elements are invalidated. no cap, theyβre gone.
Each iterator in the range rg is dereferenced exactly once. one and done, bestie.
β οΈ The behavior is undefined if rg overlaps with the container. like, do not do that. fr fr π
π Parameters π
rgβ aninput_rangewith reference type convertible to the element type of the container.
π§ Type requirements π§
std::assignable_from<T&, ranges::range_reference_t<R>>must be modeled. Otherwise, the program is ill-formed.Tmust beEmplaceConstructibleinto the container from*ranges::begin(rg). IfRmodels neithersized_rangenorforward_range,Tmust be alsoMoveInsertableinto the container. Otherwise, the behavior is undefined.
π Return value π
(none) β literally nothing, bestie. it just does the thing π«Ά
π₯ Notes π₯
Feature-test macro vibes:
- Feature-test macro:
__cpp_lib_containers_ranges - Value:
202202L - Std:
(C++23) - Feature: Ranges-aware construction and insertion
π Example π
Run this code:
#include <algorithm>
#include <cassert>
#include <vector>
#include <list>
int main()
{
const auto source = std::list{2, 7, 1};
auto destination = std::vector{3, 1, 4};
#ifdef __cpp_lib_containers_ranges
destination.assign_range(source);
#else
destination.assign(source.cbegin(), source.cend());
#endif
assert(std::ranges::equal(source, destination));
}
π See also π
insert_rangeβ inserts a range of elements (C++23) (public member function)append_rangeβ adds a range of elements to the end (C++23) (public member function)assignβ assigns values to the container (public member function)
mwah π this page came from http://cppreference.com, dated 2024.06.10, and it is literally std::vector::assign_range(3). bye bestie, go assign some ranges 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