Condividi tramite


vector::vector

Constructs a vector of a specific size or with elements of a specific value or with a specific allocator or as a copy of all or part of some other 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<Type, Allocator>& _Right
);
template<class InputIterator>
   vector(
      InputIterator _First,
      InputIterator _Last
   );
template<class InputIterator>
   vector(
      InputIterator _First,
      InputIterator _Last,
      const Allocator& _Al
   );

Parameters

  • _Al
    The allocator class to use with this object. get_allocator returns the allocator class for the object.

  • _Count
    The number of elements in the constructed vector.

  • _Val
    The value of the elements in the constructed vector.

  • _Right
    The vector of which the constructed vector is to be a copy.

  • _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.

Remarks

All constructors store an allocator object (_Al) and initialize the vector.

The first two constructors specify an empty initial vector. The second explicitly specifies the allocator type (_Al) to be used.

The third constructor specifies a repetition of a specified number (_Count) of elements of the default value for class Type.

The fourth and fifth constructors specify a repetition of (_Count) elements of value _Val.

The sixth constructor specifies a copy of the vector _Right.

The last two constructors copy the range [_First, _Last) of a vector.

Example

// 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;

   // 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 vector v5 by copying the range v4[_First, _Last)
   vector <int> v5( v4.begin( ) + 1, v4.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;   
}

v1 = 0 0 0
v2 = 2 2 2 2 2
v3 = 1 1 1
v4 = 2 2 2 2 2
v5 = 2 2

Requirements

Header: <vector>

Namespace: std

See Also

Concepts

vector Class

vector Members

Standard Template Library