C++ Standard Library C++ STL Library

C++ <vector> - vector() Function



The C++ vector::vector function is used to construct a vector object, initializing its contents depending on the version of constructor used:

Syntax

//default version - construct an empty 
//container with no elements
explicit vector (const allocator_type& alloc = allocator_type());

//fill version - construct a container 
//with n elements, each equal to val
explicit vector (size_type n, const value_type& val = value_type(),
                 const allocator_type& alloc = allocator_type());

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
         vector (InputIterator first, InputIterator last,
                 const allocator_type& alloc = allocator_type());

//copy version - copies all elements
//of x into the container	
vector (const vector& x);
//default version - construct an empty 
//container with no elements
explicit vector (const allocator_type& alloc = allocator_type());

//fill version - construct a container 
//with n elements, each equal to val	
explicit vector (size_type n);
vector (size_type n, const value_type& val,
        const allocator_type& alloc = allocator_type());

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  vector (InputIterator first, InputIterator last,
          const allocator_type& alloc = allocator_type());

//copy version - copies all elements
//of x into the container	
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);

//move version - moves elements of x
//into the container	
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container	
vector (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());
//default version - construct an empty 
//container with no elements
vector();
explicit vector (const allocator_type& alloc);

//fill version - construct a container 
//with n elements, each equal to val
explicit vector (size_type n, const allocator_type& alloc = allocator_type());
vector (size_type n, const value_type& val,
        const allocator_type& alloc = allocator_type());

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  vector (InputIterator first, InputIterator last,
          const allocator_type& alloc = allocator_type());

//copy version - copies all elements
//of x into the container	
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);

//move version - moves elements of x
//into the container	
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container		
vector (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());

Parameters

alloc Specify the Allocator object. The container keeps and uses an internal copy of this allocator.
n Specify the number of elements in the container at construction.
val Specify the value to fill the container with.
first Specify initial position of the input iterator of the range. The range used is [first,last).
last Specify final position of the input iterator of the range. The range used is [first,last).
x Specify a vector object of same type.
il Specify an initializer_list object.

Return Value

Constructor never returns value.

Time Complexity

Constant i.e, Θ(1), for default version and move version.
For all the other cases, Linear i.e, Θ(n).

Example: using default and fill version

In the example below, the vector::vector function is used to construct a vector object.

#include <iostream>
#include <vector>
using namespace std;
 
int main (){
  //default version - construct an empty vector
  vector<int> vec1;
  vector<int>::iterator it;

  //fill version - construct a vector
  //with 5 elements each equal to 100
  vector<int> vec2(5, 100);

  cout<<"vec1 contains: ";
  for(it = vec1.begin(); it != vec1.end(); ++it)
    cout<<*it<<" ";

  cout<<"\nvec2 contains: ";
  for(it = vec2.begin(); it != vec2.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

vec1 contains: 
vec2 contains: 100 100 100 100 100 

Example: using range and copy version

A vector can also be constructed using range or copy version. Consider the following example:

#include <iostream>
#include <vector>
using namespace std;
 
int main (){
  vector<int> vec1{100, 200, 300};
  vector<int>::iterator it;

  //range version - construct vec2 using range
  vector<int> vec2(vec1.begin(), vec1.end());

  //copy version - construct vec3 from vec1
  vector<int> vec3(vec1); 

  cout<<"vec2 contains: ";
  for(it = vec2.begin(); it != vec2.end(); ++it)
    cout<<*it<<" ";

  cout<<"\nvec3 contains: ";
  for(it = vec3.begin(); it != vec3.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

vec2 contains: 100 200 300
vec3 contains: 100 200 300 

Example: using move version

Using the move version of vector, the content of one vector can be moved to another vector. Consider the following example:

#include <iostream>
#include <vector>
using namespace std;
 
int main (){
  vector<int> vec1{10, 20, 30, 40, 50};
  vector<int>::iterator it;

  cout<<"vec1 contains: ";
  for(it = vec1.begin(); it != vec1.end(); ++it)
    cout<<*it<<" ";

  //moving all content of vec1 into vec2
  vector<int> vec2(move(vec1));

  cout<<"\nvec1 contains: ";
  for(it = vec1.begin(); it != vec1.end(); ++it)
    cout<<*it<<" ";

  cout<<"\nvec2 contains: ";
  for(it = vec2.begin(); it != vec2.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

vec1 contains: 10 20 30 40 50 
vec1 contains: 
vec2 contains: 10 20 30 40 50 

Example: using initializer list version

The initializer list can also be used to assign values into a vector container. Consider the example below:

#include <iostream>
#include <vector>
using namespace std;
 
int main (){
  //creating initializer list
  initializer_list<int> ilist = {15, 30, 45, 60, 75};

  //initializer list version - copies all 
  //elements of ilist into the container	
  vector<int> vec(ilist);
  vector<int>::iterator it;

  cout<<"vec contains: ";
  for(it = vec.begin(); it != vec.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

vec contains: 15 30 45 60 75 

❮ C++ <vector> Library