• Register
100 points
7 4

Vector is a standard library class template, which is similar to array, but also supports dynamic resizing. Vector provides a data structure with contiguous memory location. This enables efficient, direct access to any element of a vector via the subscript operator [ ], exactly as with a built-in array. Like class template arrays, template vector is most commonly used when the data in the container must be easily accessible via subscript or will be sorted, and when the number of elements may need to grow. This article shows how to initialize an empty vector in C++

How to initialize an empty vector in C++?

Initializing an empty vector is really pretty easy, we just have to initialize a vector without putting any values in it. Like we do while creating vectors and pushing values into them. The only difference is that now we don’t have to push values in it. Sounds easy.
Syntax of creating vector is  vector<type> vector_name;


#include <iostream>
#include <vector>
using namespace std;

int main()
    vector<int> v1;

    vector<int>::iterator it;
    cout << "Vector v1 elements are: ";
    for (it = v1.begin(); it != v1.end(); it++)
        cout << *it << " ";
    cout << endl;

    return 0;


Vector v1 elements are:

As we expected output of the vector is null since initialized an empty vector. See told ya it's easy.

100 points
7 4