共用方式為


vector::vector

建構向量特定大小或具有特定值的項目或與特定配置器或做為其他向量全部或部分的複本。

vector( ); 
explicit vector(
   const Allocator& _Al
);
explicit vector(
   size_type _Count
);
vector(
   size_type _Count,
   const Type& _Val
);
vector(
   size_type _Count,
   const Type& _Val,
   const Allocator& _Al
);
vector(
   const vector& _Right
);
template<class InputIterator>
   vector(
      InputIterator _First,
      InputIterator _Last
   );
template<class InputIterator>
   vector(
      InputIterator _First,
      InputIterator _Last,
      const Allocator& _Al
   );
vector(
   vector&& _Right
);

參數

參數

描述

_Al

搭配這個物件使用的配置器類別。 get_allocator 傳回物件的配置器類別。

_Count

項目數目所建構的向量。

_Val

項目的值在建構的向量。

_Right

已建構的向量是複本的向量。

_First

在要複製的項目的範圍中之第一個項目的位置。

_Last

超出要複製的項目的範圍之第一個項目的位置。

備註

所有建構函式所儲存的配置器物件_Al() 和初始化向量。

前兩個建構函式中指定空的初始向量。 第二個明確指定 (_Al) 所使用的配置器類型。

第三個建構函式的類別 Type指定特定數目 (_Count) 中的預設值的項目。

第四和第五建構函式中指定 (值 _Val的_Count) 項目的重複。

第六個建構函式所指定向量 _Right的複本。

七個和第八個建構函式會從複製範圍 [_First, _Last) 的向量。

最後一個建構函式會將 _Right向量。

範例

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

int main( )
{
   using namespace std;
   vector <int>::iterator v1_Iter, v2_Iter, v3_Iter, v4_Iter, v5_Iter, v6_Iter;

   // Create an empty vector v0
   vector <int> v0;

   // Create a vector v1 with 3 elements of default value 0
   vector <int> v1( 3 );

   // Create a vector v2 with 5 elements of value 2
   vector <int> v2( 5, 2);

   // Create a vector v3 with 3 elements of value 1 and with the allocator 
   // of vector v2
   vector <int> v3( 3, 1, v2.get_allocator( ) );

   // Create a copy, vector v4, of vector v2
   vector <int> v4( v2 );

   // Create a new temporary vector for demonstrating copying ranges
   vector <int> v5( 5 );
   for ( int index = 0; index < 5; index++ )
      v5[index] = index;

   // Create a vector v6 by copying the range v5[_First, _Last)
   vector <int> v6( v5.begin( ) + 1, v5.begin( ) + 3 );

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

   cout << "v3 =" ;
   for ( v3_Iter = v3.begin( ) ; v3_Iter != v3.end( ) ; v3_Iter++ )
      cout << " " << *v3_Iter;
   cout << endl;

   cout << "v4 =" ;
   for ( v4_Iter = v4.begin( ) ; v4_Iter != v4.end( ) ; v4_Iter++ )
      cout << " " << *v4_Iter;
   cout << endl;

   cout << "v5 =";
   for ( v5_Iter = v5.begin( ) ; v5_Iter != v5.end( ) ; v5_Iter++ )
      cout << " " << *v5_Iter;
   cout << endl;

   cout << "v6 =";
   for ( v6_Iter = v6.begin( ) ; v6_Iter != v6.end( ) ; v6_Iter++ )
      cout << " " << *v6_Iter;
   cout << endl;

   // Move vector v2 to vector v7
   vector <int> v7( move(v2) );
   vector <int>::iterator v7_Iter;
   
   cout << "v7 =" ;
   for ( v7_Iter = v7.begin( ) ; v7_Iter != v7.end( ) ; v7_Iter++ )
      cout << " " << *v7_Iter;
   cout << endl;
}
  

需求

標題: <vector>

命名空間: std

請參閱

參考

vector Class

標準樣板程式庫