In this tutorial, we will discuss about the vector container in C++.

What is Vector in C++?

In C++, the Standard Template Library (STL) provides a sequencal container that can resize automatically. It is also known as dynamic array because it can change its size during runtime.

The vector is a sequential container provided by the Standard Template Library (STL). It’s termed “sequential” because it stores all the elements in consecutive memory locations in the order they are inserted.

Since elements are stored in consecutive memory locations, the vector supports random access. This means we can directly access the ith element of a vector, much like we do with an array. And the best part? This is achieved in O(1) time complexity. Hence, it provides rapid random access operations.

Understanding Indexing in C++ Vectors:

Indexing in C++ starts from 0. For example,

  • The index position of the first element is 0.
  • The index position of the second element is 1.
  • For the ith element, the index position is i-1

Vector vs C-Style Array

A vector is often referred to as a “dynamic array” because of its ability to alter its size during runtime. When we instantiate a vector, it allocates a certain amount of memory, referred to as the “capacity” of the vector. For instance, if we create a vector and initially allocate memory for 10 elements, its size would be zero as it doesn’t contain any elements yet. As we add elements and the size nears its capacity, the vector allocates a larger memory segment and transfers the existing elements to this new memory space. This mechanism allows the vector to adjust its size internally. Thus, you can continually add elements to a vector without any size constraints, unlike arrays.

Important facts about Vector in C++

Let’s discuss some important facts about vector in C++.

1. Contiguous Storage

Vectors store elements in contiguous memory locations. This characteristic not only ensures efficient access, making it cache-friendly, but it also implies that insertions or deletions from the middle can be costly due to the necessity of shifting elements.

2. Dynamically Resizable

Unlike native arrays in C++, vectors are dynamic in nature. Their size can be altered during runtime. Depending on the need, they can automatically expand or contract.

3. Random Access

Owing to their contiguous memory storage, vectors facilitate constant time access (O(1)) for both reading and writing operations for indexed elements.

4. Homogeneous

Vectors are designed to store elements of a single data type. For instance, one could have a vector of int or a vector of string. However, creating a heterogeneous vector that combines different data types, like int and string, is not permissible.

Header File for using Vector in C++

To use vectors, you need to include the <vector> header.

#include <vector>

Syntax to Create a Vector

The general syntax for creating a vector is:

std::vector<Type> variableName;

Here, Type is a template parameter i.e. the data type of data that this vector object will store. It is necessary to specify the data type because vectors are a part of the STL which heavily relies on templates. A Vector object can hold elements of the given data type only.

Creating an Empty Vector

Let’s see how to create an empty vector and check its size:

std::vector<int> vecObj;

std::cout << "Size of the vector: " << vecObj.size() << std::endl;  
// Outputs: 0

The size() function returns the number of elements present in the vector.

Initializing a Vector with Default Values

You can also initialize a vector with default values. For instance, to create a vector of size 5 and initailzie it with default values, use the following code,

std::vector<int> vecObj {11, 22, 33, 4, 55};

std::cout << "Size of the initialized vector: " << vecObj.size() << std::endl;
// Outputs: 5

Iterating Over a Vector Using Range-Based For Loop

A range-based for loop provides a simpler way to iterate over the elements of collections, such as vectors:

std::vector<int> vecObj {11, 22, 33, 4, 55};

for(const auto &value : vecObj) {
    std::cout << value << " ";
// Outputs: 11 22 33 44 55}

Here, the auto keyword automatically determines the type of data in the vector. The & ensures that we’re accessing the values by reference (though in this case, since we’re only reading the values and not modifying them, the & can be optional).


In upcoming articles, we will discuss the different ways to operate on vectors in C++. Topics will include how to create them, how to add elements, how to remove elements, how to double the size of a vector, how to search for elements, and how to erase elements. Vectors also support iterators, so we will discuss how to avoid scenarios that can invalidate your iterators.

All these topics will be covered in this tutorial series on the Standard Template Library.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.