Udostępnij za pośrednictwem


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& _Right
);
template<class InputIterator>
   vector(
      InputIterator _First,
      InputIterator _Last
   );
template<class InputIterator>
   vector(
      InputIterator _First,
      InputIterator _Last,
      const Allocator& _Al
   );
vector(
   vector&& _Right
);

Parameters

Parameter

Description

_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 seventh and eighth constructors copy the range [_First, _Last) of a vector.

The last constructor moves the vector _Right.

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, 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;
}
v1 = 0 0 0
v2 = 2 2 2 2 2
v3 = 1 1 1
v4 = 2 2 2 2 2
v5 = 2 2
v6 = 1 2
v7 = 2 2 2 2 2

Requirements

Header: <vector>

Namespace: std

See Also

Reference

vector Class

Standard Template Library

Other Resources

vector Members