Bagikan melalui


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, v2, v3;
   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;

   v2.assign(v1.begin(), v1.end());
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter+)
      cout << *iter << " ";
   cout << endl;

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

Output

v1 = 10 20 30 40 50 
v2 = 10 20 30 40 50 
v3 = 4 4 4 4 4 4 4 

Requirements

Header: <vector>

Namespace: std

See Also

Reference

vector Class

Standard Template Library

Other Resources

vector Members