C++ Vector Library - vector() Function


Advertisements

Description

The C++ default constructor std::vector::vector() constructs an empty container, with zero elements. Size of this container is always zero.

The storage for container is allocated by internal allocator.

Declaration

Following is the declaration for std::vector::vector() constructor form std::vector header.

C++98

explicit vector (const allocator_type& alloc = allocator_type());

C++11

explicit vector (const allocator_type& alloc = allocator_type());

Parameters

alloc − allocator object

This allocator object is responsible for performing all memory allocation of this container. Container keeps and uses the internal copy of this container. Member type allocator_type is a internal allocator which is second parameter of the class template.

Return value

Constructor never returns value

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::vector::vector() constructor.

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
   vector<int> v1;

   cout << "size of v1 = " << v1.size() << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

size of v1 = 0
vector.htm
Advertisements