Difference between revisions of "Template:cpp/container/assign"
From cppreference.com
(Added LWG issue #320 DR.) |
m (fmt) |
||
Line 58: | Line 58: | ||
{{par|first, last|the range to copy the elements from}} | {{par|first, last|the range to copy the elements from}} | ||
{{par|ilist|initializer list to copy the values from}} | {{par|ilist|initializer list to copy the values from}} | ||
− | {{par end}} | + | {{par end}} |
===Complexity=== | ===Complexity=== | ||
Line 83: | Line 83: | ||
for (char c : characters) | for (char c : characters) | ||
std::cout << c << ' '; | std::cout << c << ' '; | ||
− | std::cout << '\n'; | + | std::cout << '\n'; |
}; | }; | ||
Line 97: | Line 97: | ||
} | } | ||
|output= | |output= | ||
− | a a a a a | + | a a a a a |
− | b b b b b b | + | b b b b b b |
C + + 1 1 | C + + 1 1 | ||
}} | }} | ||
− | + | <!--Keep these two comments to avoid extra new lines in no-DR pages--> | |
{{#switch: {{{1}}} | {{#switch: {{{1}}} | ||
|list= | |list= | ||
Line 109: | Line 109: | ||
{{dr list end}} | {{dr list end}} | ||
}} | }} | ||
− | + | <!----> | |
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
{{dsc inc|cpp/container/dsc constructor|{{{1|}}}}} | {{dsc inc|cpp/container/dsc constructor|{{{1|}}}}} | ||
{{dsc end}} | {{dsc end}} |
Revision as of 14:30, 31 July 2023
void assign( size_type count, const T& value ); |
(1) | (since {std}) |
template< class InputIt > void assign( InputIt first, InputIt last ); |
(2) | (since {std}) |
void assign( std::initializer_list<T> ilist ); |
(3) | (since C++11) |
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)
. The behavior is undefined if either argument is an iterator into *this.
This overload has the same effect as overload (1) if |
(until C++11) |
This overload participates in overload resolution only if |
(since C++11) |
3) Replaces the contents with the elements from the initializer list ilist.
All iterators, pointers and references to the elements of the container are invalidated.
Contents |
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 | - | initializer list to copy the values from |
Complexity
1) Linear in count
2) Linear in distance between first and last
3) Linear in ilist.size()
Example
The following code uses assign
to add several characters to a std::{{{1}}}<char>:
Run this code
#include <{{{1}}}> #include <iostream> #include <string> int main() { std::{{{1}}}<char> characters; auto print_{{{1}}} = [&]() { for (char c : characters) std::cout << c << ' '; std::cout << '\n'; }; characters.assign(5, 'a'); print_{{{1}}}(); const std::string extra(6, 'b'); characters.assign(extra.begin(), extra.end()); print_{{{1}}}(); characters.assign({'C', '+', '+', '1', '1'}); print_{{{1}}}(); }
Output:
a a a a a b b b b b b C + + 1 1
See also
constructs the (public member function of std::{{{1}}} )
|