In C++, vectors are used to store multiple elements dynamically in a single container. They automatically increase or decrease their size when elements are added or removed.
In this chapter, you will learn about C++ vectors, their features, and commonly used vector functions in C++.
Vectors are a part of the standard template library (STL). They are dynamic arrays that can automatically resize themselves when the elements are added or removed.

Unlike normal arrays, vectors provide flexibility because there is no need to define a fixed size in advance. Vectors store elements in contiguous memory locations and automatically allocate more memory when required.
Vectors provide several useful member functions such as push_back(), pop_back(), insert(), size(), clear(), and front() to add, remove, access, and manage elements in a vector.
It has the following syntax:
In this syntax,
If we need to create a vector, we have to include the <vector> header.:
Let us take a simple example to illustrate the C++ Vector.
Output:
Vector created
In C++, an element may be inserted into a vector via the insert() method, which takes a linear time. If we want to insert the number at the end, the push_back() method can be used. It works faster and takes only constant time.
It has the following system:
Let us take a simple example to insert elements in C++.
Output:
T p o i n t
We can access the elements using the [] operator or at() method. The [] method is fast but unsafe because it does not check whether the given index exists in the vector. We can also use the .at() method to access elements. The elements can be updated by assigning a new value using their index.
It has the following syntax:
Let us take an example to illustrate how to access and update elements in vectors.
Output:
The Accessing Value at position 4 is: u The Accessing Value at position 3 is: o The Updating Value at Position 4 is: y The Updating Value at Position 2 is: z
One of the main common problems of arrays is the need to maintain a separate variable to track their size. Vector can solve this problem by using the size() function, which helps to return the current number of elements in the vector.
It has the following Syntax:
Let us take an example to illustrate the size of vectors in C++.
Output:
The Size of Vector is: 10
We can traverse a vector using traditional for loops, range-based for loops, and iterators. We can utilize the loop and determine the size of the vector via the vector size() method to iterate via this range.
Let us take an example to illustrate how to traverse a vector in C++.
Output:
T p o i n t T e c h
The vectors allow the removal of the ultimate element or remove the elements from any condition using erase() and pop_back() functions. If we know the value of the element in the vector, we can use the find() function to find the location of this element. After that, we can use the vector pop_back() function to delete the element that takes only constant time.
It has the following syntax:
Let us take an example to illustrate how to delete elements from a vector in C++.
Output:
T p o i n
| Operation | Time Complexity |
|---|---|
| Access (index) | O(1) |
| Insertion (end) | O(1) |
| Deletion (end) | O(1) |
| Insertion (mid) | O(n) |
| Deletion (mid) | O(n) |
The vector can be passed by the value (copy), reference (modifies the original), or const reference (read-only).
It has the following syntax:
In C++, vectors can manage their storage automatically. When we insert an element and there is no capacity left, the vector resizes itself by allocating new memory, copying existing elements to the new memory, and deleting the old one. This mechanism ensures that push_back() runs in amortized constant time.
The vector can be nested to create 2D or 3D structures. It is helpful in problems related to involving matrices.
It has the following syntax:
Let us take an example to illustrate the multidimensional vector in C++.
Output:
1 2 3 4 5 6 7 8 9
Explanation
This code creates a 2D vector (matrix) with three rows and three columns, initializing it with specific values. After that, it uses nested loops to print each element in row-wise order, which creates a matrix.
In C++, vector offers several types of functions to perform different types of operations. The table below shows the function's names with their description.
| Function | Description |
|---|---|
| at() | It provides a reference to an element. |
| back() | It gives a reference to the last element. |
| front() | It gives a reference to the first element. |
| swap() | It exchanges the elements between two vectors. |
| push_back() | It adds a new element at the end. |
| pop_back() | It removes a last element from the vector. |
| empty() | It determines whether the vector is empty or not. |
| insert() | It inserts new element at the specified position. |
| erase() | It deletes the specified element. |
| resize() | It modifies the size of the vector. |
| clear() | It removes all the elements from the vector. |
| size() | It determines a number of elements in the vector. |
| capacity() | It determines the current capacity of the vector. |
| assign() | It assigns new values to the vector. |
| operator=() | It assigns new values to the vector container. |
| operator[]() | It access a specified element. |
| end() | It refers to the past-lats-element in the vector. |
| emplace() | It inserts a new element just before the position pos. |
| emplace_back() | It inserts a new element at the end. |
| rend() | It points the element preceding the first element of the vector. |
| rbegin() | It points the last element of the vector. |
| begin() | It points the first element of the vector. |
| max_size() | It returns the maximum number of elements that the vector can hold. |
| cend() | It refers to the past-last-element in the vector. |
| cbegin() | It refers to the first element of the vector. |
| crbegin() | It refers to the last character of the vector. |
| crend() | It refers to the element preceding the first element of the vector. |
| data() | It writes the data of the vector into an array. |
| shrink_to_fit() | It reduces the capacity and makes it equal to the size of the vector. |
Let's look at the program to demonstrate various vector functions in C++.
Output:
Vector elements: 10 15 17 25 30 40 50 Size: 7 Capacity: 7 Max Size: 1073741823 After deletion: 15 17 25 30 40 Front: 15 Back: 40 At(1): 17 After swap v1: 100 200 Is v1 empty? Yes After assign: 99 99 99 First element via data(): 99
In C++, arrays and vectors are used to store collections of similar data types, but they differ in structure, flexibility and memory management.
| Feature | Array | Vector |
|---|---|---|
| Size | It can be fixed at compile time. | It can change dynamically at runtime. |
| Memory Management | Manual | Handled automatically by vector |
| Flexibility | Low | High (functions like push_back, insert) |
| STL Compatibility | Limited | Fully compatible |
| Bound Checking | Not provided | at() method offers bound checking |
| Initialization | int arr[5]; | vector<int> v(5); |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India