Difference between revisions of "Template:cpp/container/assign"
From cppreference.com
(Added LWG issue #2209 DR.) |
|||
(25 intermediate revisions by 12 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{#vardefine:cont|{{{1|inplace_vector}}}}}<!-- |
− | {{cpp/container/{{ | + | -->{{cpp/container/{{#var:cont}}/title|assign}} |
− | {{ | + | {{cpp/container/{{#var:cont}}/navbar}} |
− | {{ | + | {{dcl begin}} |
+ | {{#switch:{{#var:cont}} | ||
+ | |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 ); | ||
}} | }} | ||
− | {{ | + | |inplace_vector= |
+ | {{dcl|num=1|since={{cpp/std|{{#var:cont}}}}| | ||
+ | constexpr void assign( size_type count, const T& value ); | ||
+ | }} | ||
+ | {{dcl|num=2|since={{cpp/std|{{#var:cont}}}}| | ||
+ | template< class InputIt > | ||
+ | constexpr void assign( InputIt first, InputIt last ); | ||
+ | }} | ||
+ | {{dcl|num=3|since={{cpp/std|{{#var:cont}}}}| | ||
+ | constexpr void assign( std::initializer_list<T> ilist ); | ||
+ | }} | ||
+ | | | ||
+ | {{dcl|num=1|since={{cpp/std|{{#var:cont}}}}| | ||
+ | void assign( size_type count, const T& value ); | ||
+ | }} | ||
+ | {{dcl|num=2|since={{cpp/std|{{#var:cont}}}}| | ||
+ | template< class InputIt > | ||
+ | void assign( InputIt first, InputIt last ); | ||
+ | }} | ||
+ | {{dcl|num=3|since=c++11| | ||
+ | void assign( std::initializer_list<T> ilist ); | ||
+ | }} | ||
+ | }} | ||
+ | {{dcl end}} | ||
Replaces the contents of the container. | Replaces the contents of the container. | ||
− | 1 | + | @1@ Replaces the contents with {{c|count}} copies of value {{c|value}}. |
− | 2 | + | @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}} | ||
+ | |inplace_vector= | ||
+ | {{cpp/enable if|{{tt|InputIt}} satisfies {{named req|InputIterator}}}}. | ||
+ | | | ||
+ | {{rrev multi|until1=c++11 | ||
+ | |rev1=This overload has the same effect as overload {{v|1}} if {{tt|InputIt}} is an integral type. | ||
+ | |rev2={{cpp/enable if|{{tt|InputIt}} satisfies {{named req|InputIterator}}}}. | ||
+ | }} | ||
+ | }} | ||
+ | @3@ Replaces the contents with the elements from {{c|ilist}}. | ||
− | + | {{cpp/container/note iterator invalidation|{{#var:cont}}|assign}} | |
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par|count|the new size of the container}} |
− | {{ | + | {{par|value|the value to initialize elements of the container with}} |
− | {{ | + | {{par|first, last|the range to copy the elements from}} |
− | {{ | + | {{par|ilist|{{lc|std::initializer_list}} to copy the values from}} |
− | {{ | + | {{par end}} |
− | + | ||
− | + | ||
===Complexity=== | ===Complexity=== | ||
+ | @1@ Linear in {{c|count}}. | ||
− | + | @2@ Linear in distance between {{c|first}} and {{c|last}}. | |
− | + | @3@ Linear in {{c|ilist.size()}}. | |
+ | <!----> | ||
+ | {{#switch:{{#var:cont}} | ||
+ | |inplace_vector= | ||
+ | ===Exceptions=== | ||
+ | @1@ {{lc|std::bad_alloc}}, if {{c|1=count > 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()}}. | ||
+ | |||
+ | @1-3@ Any exception thrown by initialization of inserted elements. | ||
+ | }} | ||
+ | <!----> | ||
===Example=== | ===Example=== | ||
+ | {{#switch:{{#var:cont}} | ||
+ | |inplace_vector= | ||
{{example | {{example | ||
− | + | |The following code uses {{tt|assign}} to add several characters to a {{c/core|std::{{#var:cont}}<char, 5>}}: | |
− | + | |code= | |
− | #include <{{{1}}}> | + | #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 | ||
+ | }} | ||
+ | | | ||
+ | {{example | ||
+ | |The following code uses {{tt|assign}} to add several characters to a {{c/core|std::{{#var:cont}}<char>}}: | ||
+ | |code= | ||
+ | #include <{{#var:cont}}> | ||
#include <iostream> | #include <iostream> | ||
− | + | #include <string> | |
+ | |||
int main() | int main() | ||
{ | { | ||
− | std::{{ | + | std::{{#var:cont}}<char> characters; |
− | + | ||
+ | auto print_{{#var:cont}} = [&]() | ||
+ | { | ||
+ | for (char c : characters) | ||
+ | std::cout << c << ' '; | ||
+ | std::cout << '\n'; | ||
+ | }; | ||
+ | |||
characters.assign(5, 'a'); | characters.assign(5, 'a'); | ||
− | + | print_{{#var:cont}}(); | |
− | + | ||
− | + | const std::string extra(6, 'b'); | |
− | } | + | characters.assign(extra.begin(), extra.end()); |
− | + | print_{{#var:cont}}(); | |
− | + | ||
+ | characters.assign({'C', '+', '+', '1', '1'}); | ||
+ | print_{{#var:cont}}(); | ||
} | } | ||
− | + | |output= | |
− | a | + | a a a a a |
− | a | + | b b b b b b |
− | a | + | C + + 1 1 |
− | a | + | |
− | + | ||
}} | }} | ||
− | + | }} | |
+ | <!-- Keep these two comments to avoid extra new lines for inplace_vector --> | ||
+ | {{#ifeq:{{#var:cont}}|inplace_vector|| | ||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{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}} | ||
+ | }} | ||
+ | <!----> | ||
===See also=== | ===See also=== | ||
− | + | {{dsc begin}} | |
− | {{ | + | {{dsc inc|cpp/container/dsc assign_range|{{#var:cont}}}} |
− | {{ | + | {{dsc inc|cpp/container/dsc operator{{=}}|{{#var:cont}}}} |
− | {{ | + | {{dsc 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> )
|