Difference between revisions of "Template:cpp/container/assign"
From cppreference.com
m (+inplace_vector() |
(Added LWG issue #2209 DR.) |
||
Line 5: | Line 5: | ||
{{#switch:{{#var:cont}} | {{#switch:{{#var:cont}} | ||
|vector= | |vector= | ||
− | {{ | + | {{dcla|num=1|constexpr=c++20| |
void assign( size_type count, const T& value ); | void assign( size_type count, const T& value ); | ||
}} | }} | ||
− | {{ | + | {{dcla|num=2|constexpr=c++20| |
template< class InputIt > | template< class InputIt > | ||
void assign( InputIt first, InputIt last ); | void assign( InputIt first, InputIt last ); | ||
}} | }} | ||
− | {{ | + | {{dcla|num=3|since=c++11|constexpr=c++20| |
void assign( std::initializer_list<T> ilist ); | void assign( std::initializer_list<T> ilist ); | ||
}} | }} | ||
Line 44: | Line 44: | ||
@1@ Replaces the contents with {{c|count}} copies of value {{c|value}}. | @1@ Replaces the contents with {{c|count}} copies of value {{c|value}}. | ||
− | @2@ Replaces the contents with copies of those in the range {{range|first|last}}. | + | @2@ Replaces the contents with copies of those in the range {{range|first|last}}. |
+ | @@ If either argument is an iterator into {{c|*this}}, the behavior is undefined. | ||
{{#switch:{{#var:cont}} | {{#switch:{{#var:cont}} | ||
|inplace_vector= | |inplace_vector= | ||
Line 54: | Line 55: | ||
}} | }} | ||
}} | }} | ||
− | @3@ Replaces the contents with the elements from | + | @3@ Replaces the contents with the elements from {{c|ilist}}. |
− | {{cpp/container/ | + | {{cpp/container/note iterator invalidation|{{#var:cont}}|assign}} |
===Parameters=== | ===Parameters=== | ||
Line 63: | Line 64: | ||
{{par|value|the value to initialize elements of the container with}} | {{par|value|the value to initialize elements of the container with}} | ||
{{par|first, last|the range to copy the elements from}} | {{par|first, last|the range to copy the elements from}} | ||
− | {{par|ilist| | + | {{par|ilist|{{lc|std::initializer_list}} to copy the values from}} |
{{par end}} | {{par end}} | ||
Line 77: | Line 78: | ||
===Exceptions=== | ===Exceptions=== | ||
@1@ {{lc|std::bad_alloc}}, if {{c|1=count > capacity()}}. | @1@ {{lc|std::bad_alloc}}, if {{c|1=count > capacity()}}. | ||
+ | |||
@2@ {{lc|std::bad_alloc}}, if {{c|1=std::ranges::distance(first, last) > capacity()}}. | @2@ {{lc|std::bad_alloc}}, if {{c|1=std::ranges::distance(first, last) > capacity()}}. | ||
+ | |||
@3@ {{lc|std::bad_alloc}}, if {{c|1=ilist.size() > capacity()}}. | @3@ {{lc|std::bad_alloc}}, if {{c|1=ilist.size() > capacity()}}. | ||
+ | |||
@1-3@ Any exception thrown by initialization of inserted elements. | @1-3@ Any exception thrown by initialization of inserted elements. | ||
}} | }} | ||
Line 96: | Line 100: | ||
{ | { | ||
std::inplace_vector<char, 5> chars; | std::inplace_vector<char, 5> chars; | ||
− | + | ||
chars.assign(4, 'a'); // overload (1) | chars.assign(4, 'a'); // overload (1) | ||
std::println("{}", chars); | std::println("{}", chars); | ||
− | + | ||
const char extra[3]{'a', 'b', 'c'}; | const char extra[3]{'a', 'b', 'c'}; | ||
chars.assign(std::cbegin(extra), std::cend(extra)); // overload (2) | chars.assign(std::cbegin(extra), std::cend(extra)); // overload (2) | ||
std::println("{}", chars); | std::println("{}", chars); | ||
− | + | ||
chars.assign({'C', '+', '+', '2', '6'}); // overload (3) | chars.assign({'C', '+', '+', '2', '6'}); // overload (3) | ||
std::println("{}", chars); | std::println("{}", chars); | ||
− | + | ||
try | try | ||
{ | { | ||
Line 112: | Line 116: | ||
} | } | ||
catch(const std::bad_alloc&) { std::println("std::bad_alloc #1"); } | catch(const std::bad_alloc&) { std::println("std::bad_alloc #1"); } | ||
− | + | ||
try | try | ||
{ | { | ||
Line 119: | Line 123: | ||
} | } | ||
catch(const std::bad_alloc&) { std::println("std::bad_alloc #2"); } | catch(const std::bad_alloc&) { std::println("std::bad_alloc #2"); } | ||
− | + | ||
try | try | ||
{ | { | ||
Line 146: | Line 150: | ||
{ | { | ||
std::{{#var:cont}}<char> characters; | std::{{#var:cont}}<char> characters; | ||
− | + | ||
auto print_{{#var:cont}} = [&]() | auto print_{{#var:cont}} = [&]() | ||
{ | { | ||
Line 153: | Line 157: | ||
std::cout << '\n'; | std::cout << '\n'; | ||
}; | }; | ||
− | + | ||
characters.assign(5, 'a'); | characters.assign(5, 'a'); | ||
print_{{#var:cont}}(); | print_{{#var:cont}}(); | ||
− | + | ||
const std::string extra(6, 'b'); | const std::string extra(6, 'b'); | ||
characters.assign(extra.begin(), extra.end()); | characters.assign(extra.begin(), extra.end()); | ||
print_{{#var:cont}}(); | print_{{#var:cont}}(); | ||
− | + | ||
characters.assign({'C', '+', '+', '1', '1'}); | characters.assign({'C', '+', '+', '1', '1'}); | ||
print_{{#var:cont}}(); | print_{{#var:cont}}(); | ||
Line 170: | Line 174: | ||
}} | }} | ||
}} | }} | ||
− | <!--Keep these two comments to avoid extra new lines | + | <!-- Keep these two comments to avoid extra new lines for inplace_vector --> |
− | {{# | + | {{#ifeq:{{#var:cont}}|inplace_vector|| |
− | | | + | |
===Defect reports=== | ===Defect reports=== | ||
{{dr list begin}} | {{dr list begin}} | ||
− | {{dr list item|wg=lwg|dr=320|std=C++98|before=the replacement operation was | + | {{dr list item|wg=lwg|dr={{#ifeq:{{#var:cont}}|list|320|2209}}|std={{#ifeq:{{#var:cont}} |
+ | |forward_list|C++11|C++98}}|before=the replacement operation was required to be implemented as<br>erasing all existing elements followed by inserting the given elements|after=removed the<br>requirement}} | ||
{{dr list end}} | {{dr list end}} | ||
}} | }} |
Latest revision as of 22:28, 14 November 2024
constexpr void assign( size_type count, const T& value ); |
(1) | (since C++26) |
template< class InputIt > constexpr void assign( InputIt first, InputIt last ); |
(2) | (since C++26) |
constexpr void assign( std::initializer_list<T> ilist ); |
(3) | (since C++26) |
Replaces the contents of the container.
1) Replaces the contents with count copies of value value.
2) Replaces the contents with copies of those in the range
[
first,
last)
. If either argument is an iterator into *this, the behavior is undefined.
This overload participates in overload resolution only if
InputIt
satisfies LegacyInputIterator.3) Replaces the contents with the elements from ilist.
This section is incomplete |
Contents |
[edit] Parameters
count | - | the new size of the container |
value | - | the value to initialize elements of the container with |
first, last | - | the range to copy the elements from |
ilist | - | std::initializer_list to copy the values from |
[edit] Complexity
1) Linear in count.
2) Linear in distance between first and last.
3) Linear in ilist.size().
Exceptions
1-3) Any exception thrown by initialization of inserted elements.
[edit] Example
The following code uses assign
to add several characters to a std::inplace_vector<char, 5>:
Run this code
#include <inplace_vector> #include <iterator> #include <new> #include <print> int main() { std::inplace_vector<char, 5> chars; chars.assign(4, 'a'); // overload (1) std::println("{}", chars); const char extra[3]{'a', 'b', 'c'}; chars.assign(std::cbegin(extra), std::cend(extra)); // overload (2) std::println("{}", chars); chars.assign({'C', '+', '+', '2', '6'}); // overload (3) std::println("{}", chars); try { chars.assign(8, 'x'); // throws: count > chars.capacity() } catch(const std::bad_alloc&) { std::println("std::bad_alloc #1"); } try { const char bad[8]{'?'}; // ranges::distance(bad) > chars.capacity() chars.assign(std::cbegin(bad), std::cend(bad)); // throws } catch(const std::bad_alloc&) { std::println("std::bad_alloc #2"); } try { const auto l = {'1', '2', '3', '4', '5', '6'}; chars.assign(l); // throws: l.size() > chars.capacity() } catch(const std::bad_alloc&) { std::println("std::bad_alloc #3"); } }
Output:
['a', 'a', 'a', 'a'] ['a', 'b', 'c'] ['C', '+', '+', '2', '6'] std::bad_alloc #1 std::bad_alloc #2 std::bad_alloc #3
[edit] See also
assigns a range of 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> )
|