std::deque::assign_range(3)
π std::deque::assign_range(3) π
okay so bestie, welcome to the std::deque::assign_range page from the C++ Standard Library β yes that C++ Standard Library, the one with the manual name std::deque::assign_range(3). grab a lil snack π, we're getting into it.
β¨ NAME β¨
std::deque::assign_range β literally what it says on the tin: std::deque::assign_range. no cap, that's the whole name section π
πΈ SYNOPSIS πΈ
template< container-compatible-range<T> R > (since C++23)
void assign_range( R&& rg );
so what does it do? it replaces elements in the container with a copy of each element in rg. that's the whole main character move π
and now the serious part, because this is important β all iterators (including the end() iterator) and all references to the elements are invalidated. π like, all of them. gone. do not hold onto them, bestie, they will betray you.
Each iterator in the range rg is dereferenced exactly once. one and done, no double-dipping π«Ά
β οΈ The behavior is undefined if rg overlaps with the container. fr fr, keep them separate, this is not a drill π
π 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). Otherwise, the behavior is undefined. π
π Return value π
(none) β nothing, nada, it's giving void. don't @ me β¨
π₯ Notes π₯
okay so there's a feature-test macro you can vibe-check for this one:
__cpp_lib_containers_rangesβ value202202L, std(C++23), feature Ranges-aware construction and insertion π
β Example β
// Run this code
#include <algorithm>
#include <cassert>
#include <deque>
#include <list>
int main()
{
const auto source = std::list{2, 7, 1};
auto destination = std::deque{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 how it falls back to destination.assign(source.cbegin(), source.cend()); when the macro isn't there? that's the graceful era-appropriate backup, we love a portable icon π
π§ See also π§
insert_rangeβ inserts a range of elements (C++23) (public member function)prepend_rangeβ adds a range of elements to the beginning (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)
and that's the page!! π source is http://cppreference.com, dated 2024.06.10, and this whole little manual page is std::deque::assign_range(3). mwah, go invalidate some iterators 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