# std::forward_list<T,Allocator>::assign_range

From cppreference.com
< cpp‎ | container‎ | forward list

template< container-compatible-range<T> R > void assign_range( R&& rg ); |  | (since C++23)

Replaces elements in the container with a copy of each element in rg.

Each iterator in the range rg is dereferenced exactly once.

The behavior is undefined if rg overlaps with the container.

### 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). Otherwise, the behavior is undefined.

### Return value

(none)

### Notes

Feature-test macro | Value | Std | Comment
--- | --- | --- | ---
`__cpp_lib_containers_ranges` | 202202L | (C++23) | Ranges-aware construction and insertion

### Example

Run this code

    #include <algorithm>
    #include <cassert>
    #include <forward_list>
    #include <list>

    int main()
    {
        const auto source = std::list{2, 7, 1};
        auto destination = std::forward_list{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_after (C++23) | inserts a range of elements after an element (public member function)
prepend_range (C++23) | adds a range of elements to the beginning (public member function)
assign (C++11) | assigns values to the container (public member function)

Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/forward_list/assign_range&oldid=154714"