다음을 통해 공유


vector::operator=

Replaces the elements of the vector with a copy of another vector.

vector& operator=(
   const vector& _Right
);
vector& operator=(
   vector&& _Right
);

Parameters

Parameter

Description

_Right

The vector Class being copied into the vector.

Remarks

After erasing any existing elements in a vector, operator= either copies or moves the contents of _Right into the vector.

Example

// vector_operator_as.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 = v1;
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter++)
      cout << *iter << " ";
   cout << endl;

// move v1 into v2
   v2.clear();
   v2 = move(v1);
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter++)
      cout << *iter << " ";
   cout << endl;
}

Output

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

Requirements

Header: <vector>

Namespace: std

See Also

Reference

vector Class

Standard Template Library

Other Resources

vector Members