Export (0) Print
Expand All

vector::assign

Erases a vector and copies the specified elements to the empty vector.

void assign(
   size_type _Count,
   const Type& _Val
);
template<class InputIterator>
   void assign(
      InputIterator _First,
      InputIterator _Last
   );

Parameters

_First
Position of the first element in the range of elements to be copied.
_Last
Position of the first element beyond the range of elements to be copied.
_Count
The number of copies of an element being inserted into the vector.
_Val
The value of the element being inserted into the vector.

Remarks

After erasing any existing elements in a vector, assign either inserts a specified range of elements from the original vector into a vector or inserts copies of a new element of a specified value into a vector.

Example

// vector_assign.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;   
   vector <int> v1;
   vector <int>::iterator Iter;
   
   v1.push_back( 10 );
   v1.push_back( 20 );
   v1.push_back( 30 );
   v1.push_back( 40 );
   v1.push_back( 50 );

   cout << "v1 = " ;
   for ( Iter = v1.begin( ); Iter != v1.end( ); Iter++ )
      cout << *Iter << " ";
   cout << endl;

   v1.assign( v1.begin( ) + 1, v1.begin( ) + 3 );
   cout << "v1 = ";
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << *Iter << " ";
   cout << endl;

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

Output

v1 = 10 20 30 40 50

v1 = 20 30

v1 = 4 4 4 4 4 4 4

See Also

vector Class | vector Members

Show:
© 2014 Microsoft