Difference between revisions of "Template:cpp/container/assign range"
From cppreference.com
m (Use the {{cpp/container/note iterator invalidation}} hub; WIP.) |
m (+inplace_vector() |
||
(2 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{#vardefine:cont|{{{1|inplace_vector}}}}}<!-- |
− | {{cpp/container/{{ | + | -->{{cpp/container/{{#var:cont}}/title|assign_range}} |
− | + | {{cpp/container/{{#var:cont}}/navbar}} | |
− | {{ddcl|since=c++23| | + | {{ddcl|since={{#switch:{{#var:cont}}|inplace_vector=c++26|c++23}}| |
template< container-compatible-range<T> R > | template< container-compatible-range<T> R > | ||
− | {{# | + | {{#switch:{{#var:cont}}|inplace_vector|vector=constexpr void|void}} assign_range( R&& rg ); |
}} | }} | ||
Replaces elements in the container with a copy of each element in {{c|rg}}. | Replaces elements in the container with a copy of each element in {{c|rg}}. | ||
− | {{cpp/container/note iterator invalidation|{{ | + | {{cpp/container/note iterator invalidation|{{#var:cont}}|assign_range}} |
Each iterator in the range {{c|rg}} is dereferenced exactly once. | Each iterator in the range {{c|rg}} is dereferenced exactly once. | ||
Line 20: | Line 20: | ||
{{par hreq}} | {{par hreq}} | ||
{{par req|{{c|std::assignable_from<T&, ranges::range_reference_t<R>>}} must be modeled. Otherwise, the program is ill-formed.}} | {{par req|{{c|std::assignable_from<T&, ranges::range_reference_t<R>>}} must be modeled. Otherwise, the program is ill-formed.}} | ||
− | {{par req|{{tt|T}} must be {{named req|EmplaceConstructible}} into the container from {{c|*ranges::begin(rg)}}{{# | + | {{par req|{{tt|T}} must be {{named req|EmplaceConstructible}} into the container from {{c|*ranges::begin(rg)}}{{#switch:{{#var:cont}}|vector=. If {{tt|R}} models neither {{lconcept|sized_range}} nor {{lconcept|forward_range}}, {{tt|T}} must be also {{named req|MoveInsertable}} into the container}}. Otherwise, the behavior is undefined.}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
(none) | (none) | ||
− | + | <!----> | |
+ | {{#switch:{{#var:cont}}|inplace_vector= | ||
+ | ===Exceptions=== | ||
+ | * {{lc|bad_alloc}}, if {{c|1=std::ranges::distance(rg) > capacity()}}. | ||
+ | * Any exception thrown by initialization of inserted element. | ||
+ | }} | ||
+ | <!----> | ||
+ | {{#switch:{{#var:cont}}|inplace_vector=| | ||
===Notes=== | ===Notes=== | ||
{{feature test macro|__cpp_lib_containers_ranges|[[cpp/ranges/to#container compatible range|Ranges-aware]] construction and insertion|value=202202L|std=C++23}} | {{feature test macro|__cpp_lib_containers_ranges|[[cpp/ranges/to#container compatible range|Ranges-aware]] construction and insertion|value=202202L|std=C++23}} | ||
− | + | }} | |
+ | <!----> | ||
===Example=== | ===Example=== | ||
− | {{example|code= | + | {{#switch:{{#var:cont}} |
+ | |inplace_vector= | ||
+ | {{example | ||
+ | |code= | ||
#include <algorithm> | #include <algorithm> | ||
#include <cassert> | #include <cassert> | ||
− | #include < | + | #include <initializer_list> |
− | #include < | + | #include <inplace_vector> |
+ | #include <iostream> | ||
+ | #include <new> | ||
int main() | int main() | ||
{ | { | ||
− | const auto source = | + | const auto source = {1, 2, 3}; |
− | + | std::inplace_vector<int, 4> destination{4, 5}; | |
destination.assign_range(source); | destination.assign_range(source); | ||
+ | assert(std::ranges::equal(destination, source)); | ||
+ | |||
+ | try | ||
+ | { | ||
+ | const auto bad = {-1, -2, -3, -4, -5}; | ||
+ | destination.assign_range(bad); // throws: bad.size() > destination.capacity() | ||
+ | } | ||
+ | catch(const std::bad_alloc& ex) | ||
+ | { | ||
+ | std::cout << ex.what() << '\n'; | ||
+ | } | ||
+ | } | ||
+ | |p=true | ||
+ | |output= | ||
+ | std::bad_alloc | ||
+ | }} | ||
+ | | | ||
+ | {{example | ||
+ | |code= | ||
+ | #include <algorithm> | ||
+ | #include <cassert> | ||
+ | #include <{{#var:cont}}> | ||
+ | #include <{{#ifeq:{{#var:cont}}|list|vector|list}}> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | const auto source = std::{{#ifeq:{{#var:cont}}|list|vector|list}}{2, 7, 1}; | ||
+ | auto destination = std::{{#var:cont}}{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)); | assert(std::ranges::equal(source, destination)); | ||
} | } | ||
+ | }} | ||
}} | }} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}}|forward_list= |
− | {{cpp/container/dsc insert_range_after|{{ | + | {{cpp/container/dsc insert_range_after|{{#var:cont}}}} |
− | |{{cpp/container/dsc insert_range|{{ | + | |{{cpp/container/dsc insert_range|{{#var:cont}}}} |
}} | }} | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}}|deque|forward_list|list= |
− | {{cpp/container/dsc prepend_range|{{ | + | {{cpp/container/dsc prepend_range|{{#var:cont}}}} |
}} | }} | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}}|deque|list|inplace_vector|vector= |
− | {{cpp/container/dsc append_range|{{ | + | {{cpp/container/dsc append_range|{{#var:cont}}}} |
}} | }} | ||
− | {{cpp/container/dsc assign|{{{ | + | {{cpp/container/dsc assign|{{#var:cont}}}} |
+ | {{cpp/container/dsc operator{{=}}|{{#var:cont}}}} | ||
{{dsc end}} | {{dsc end}} |
Latest revision as of 12:56, 18 August 2024
template< container-compatible-range<T> R > constexpr void assign_range( R&& rg ); |
(since C++26) | |
Replaces elements in the container with a copy of each element in rg.
This section is incomplete |
Each iterator in the range rg is dereferenced exactly once.
The behavior is undefined if rg overlaps with the container.
Contents |
[edit] 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.
|
[edit] Return value
(none)
Exceptions
- bad_alloc, if std::ranges::distance(rg) > capacity().
- Any exception thrown by initialization of inserted element.
[edit] Example
Run this code
#include <algorithm> #include <cassert> #include <initializer_list> #include <inplace_vector> #include <iostream> #include <new> int main() { const auto source = {1, 2, 3}; std::inplace_vector<int, 4> destination{4, 5}; destination.assign_range(source); assert(std::ranges::equal(destination, source)); try { const auto bad = {-1, -2, -3, -4, -5}; destination.assign_range(bad); // throws: bad.size() > destination.capacity() } catch(const std::bad_alloc& ex) { std::cout << ex.what() << '\n'; } }
Possible output:
std::bad_alloc
[edit] See also
inserts a range of elements (public member function of std::inplace_vector<T,N> )
| |
adds a range of elements to the end (public member function of std::inplace_vector<T,N> )
| |
assigns values to the container (public member function of std::inplace_vector<T,N> )
| |
assigns values to the container (public member function of std::inplace_vector<T,N> )
|