list::assign

Erases elements from a list and copies a new set of elements to a target list.

template<class InputIterator>
   void assign(
      InputIterator _First,
      InputIterator _Last
   );

void assign(
   size_type _Count,
   const Type& _Val
);

Parameters

  • _First
    Position of the first element in the range of elements to be copied from the argument list.

  • _Last
    Position of the first element just beyond the range of elements to be copied from the argument list.

  • _Count
    The number of copies of an element being inserted into the list.

  • _Val
    The value of the element being inserted into the list.

Remarks

After erasing any existing elements in the target list, assign either inserts a specified range of elements from the original list or from some other list into the target list or inserts copies of a new element of a specified value into the target list

Example

// list_assign.cpp
// compile with: /EHsc
#include <list>
#include <iostream>

int main( ) 
{
   using namespace std;
   list<int> c1, c2;
   list<int>::const_iterator cIter;
   
   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );
   c2.push_back( 40 );
   c2.push_back( 50 );
   c2.push_back( 60 );

   cout << "c1 =";
   for ( cIter = c1.begin( ); cIter != c1.end( ); cIter++ )
      cout << " " << *cIter;
   cout << endl;

   c1.assign( ++c2.begin( ), c2.end( ) );
   cout << "c1 =";
   for ( cIter = c1.begin( ); cIter != c1.end( ); cIter++ )
      cout << " " << *cIter;
   cout << endl;

   c1.assign( 7, 4 );
   cout << "c1 =";
   for ( cIter = c1.begin( ); cIter != c1.end( ); cIter++ )
      cout << " " << *cIter;
   cout << endl;
}

Output

c1 = 10 20 30
c1 = 50 60
c1 = 4 4 4 4 4 4 4

Requirements

Header: <list>

Namespace: std

See Also

Concepts

list Class

list Class Members

list::assign (STL Samples)

Standard Template Library