Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
list
comparison operators
list::list
list::~list
member functions:
list::assign
list::back
list::begin
list::clear
list::empty
list::end
list::erase
list::front
list::get_allocator
list::insert
list::max_size
list::merge
list::operator=
list::pop_back
list::pop_front
list::push_back
list::push_front
list::rbegin
list::remove
list::remove_if
list::rend
list::resize
list::reverse
list::size
list::sort
list::splice
list::swap
list::unique


list::assign

public member function
template <class InputIterator>
  void assign ( InputIterator first, InputIterator last );
void assign ( size_type n, const T& u );

Assign new content to container

Assigns new content to the container, dropping all the elements contained in the container object before the call and replacing them by those specified by the parameters:

In the first version (with iterators), the new contents of the container object is a copy of those contained in the sequence between first and last (in the range [first,last)).

In the second version, the new content is the repetition n times of copies of element u.

Parameters

first, last
Input iterators to the initial and final positions in a sequence. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last.
The template type can be any type of input iterator.
n
Times that u is repeated to form the new content of the object.
Member type size_type is an unsigned integral type.
u
Value to be repeated n times as the new content of the object.
T is the first class template parameter (the type of the elements stored in the list container).

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// list::assign
#include <iostream>
#include <list>
using namespace std;
int main ()
{
  list<int> first;
  list<int> second;
  first.assign (7,100);                      // 7 ints with value 100
  second.assign (first.begin(),first.end()); // a copy of first
  int myints[]={1776,7,4};
  first.assign (myints,myints+3);            // assigning from array
  cout << "Size of first: " << int (first.size()) << endl;
  cout << "Size of second: " << int (second.size()) << endl;
  return 0;
}

Output:
Size of first: 3
Size of second: 7

Complexity

Linear on initial and final sizes (destruction, copy construction).

See also