On this page
std::list<T,Allocator>::assign
|
(1) | |
|
(2) | |
|
(3) | (since C++11) |
Replaces the contents of the container.
count
copies of value value
.
[
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) |
ilist
.
All iterators, pointers and references to the elements of the container are invalidated.
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
count
.
first
and last
.
ilist.size()
.
Example
The following code uses assign
to add several characters to a std::list<char>:
#include <list>
#include <iostream>
#include <string>
int main()
{
std::list<char> characters;
auto print_list = [&]()
{
for (char c : characters)
std::cout << c << ' ';
std::cout << '\n';
};
characters.assign(5, 'a');
print_list();
const std::string extra(6, 'b');
characters.assign(extra.begin(), extra.end());
print_list();
characters.assign({'C', '+', '+', '1', '1'});
print_list();
}
Output:
a a a a a
b b b b b b
C + + 1 1
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 320 | C++98 | the replacement operation was defined as erasing all existing elements followed by inserting the given elements |
removed the definition |
See also
constructs the list (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/container/list/assign