vector::swap

交换两个矢量的元素。

void swap(
   vector<Type, Allocator>& _Right
);
friend void swap(
   vector<Type, Allocator >& _Left,
   vector<Type, Allocator >& _Right
);

参数

  • _Right
    组件将交换与该向量 _Left的向量提供元素的将交换或矢量。

  • _Left
    组件将交换与该向量 _Right的矢量。

示例

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

int main( )
{
   using namespace std;   
   vector <int> v1, v2;

   v1.push_back( 1 );
   v1.push_back( 2 );
   v1.push_back( 3 );

   v2.push_back( 10 );
   v2.push_back( 20 );

   cout << "The number of elements in v1 = " << v1.size( ) << endl;
   cout << "The number of elements in v2 = " << v2.size( ) << endl;
   cout << endl;

   v1.swap( v2 );

   cout << "The number of elements in v1 = " << v1.size( ) << endl;
   cout << "The number of elements in v2 = " << v2.size( ) << endl;
}
  

要求

标头: <vector>

命名空间: std

请参见

参考

vector Class

标准模板库