std::vector::assign_range(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 β€” an input_range with 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.
  • T must be EmplaceConstructible into the container from *ranges::begin(rg). If R models neither sized_range nor forward_range, T must be also MoveInsertable into 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