Vector back() function

Last Updated : 27 Mar 2026

In the C++ programming language, the vector back() function is a built-in function of the vector container in the Standard Template Library (STL). It is commonly used to retrieve the last element of the vector. It gives a reference to the last element that allows us to read or modify it directly. The vector back() function is used to access the last element of a vector directly without using indexing.

cpp vector back() function

Syntax of vector back() Function

It has the following syntax:

Parameter

  • This function does not contain any parameters.

Return value

  • This function returns the last element of the vector.

Different Examples of Vector back() Function

Here, we are going to take different examples of the vector back() function in C++.

Example 1: Accessing the Last Element

Let us take an example to demonstrate how to access the last element of the vector using the vector back() function.

Output:

Explanation:

In the given example, we have taken a vector fruit that is initialized with three string elements. After that, we use the back() function to access and print the last element of the vector, which is "banana". This example demonstrates how the back() function gives direct access to the final element without using indexing.

Example 2: Modifying Last Element

Let us take an example to demonstrate how to modify the last element in the vector using the vector back() function.

Output:

Explanation:

In the given example, we have taken a vector named nums that is initialized with three elements. After that, we use the back() function to access and modify the last element of the vector by assigning it a new value (100). After completing the modification operation, a loop prints all elements, which displays that the last value has been successfully updated.

Example 3: Vector back() with Empty Vectors

Let us take an example to demonstrate the working of the back() function with empty vectors.

Output:

The vector is empty. There is no value!

Explanation:

In the given example, we have created an empty vector vec, and also take a condition to check whether the vector is empty before calling the back() function. When we call the back() function on an empty vector, it can cause undefined behavior. Therefore, the program safely avoids it using the vec.empty() function. If the vector is empty, it prints a message instead of accessing any element.

Example 4: All functionality of the vector back() function

Let us take an example to demonstrate all the functionality of the vector back() function.

Output:

Initial last element is: 30
After modification the last element is: 50
After push_back(150), the last element is: 150
After pop_back(), the last element is: 50
Current vector elements: 10 15 20 25 50 
Empty vector has no last element

Explanation:

In the given example, the back() function retrieves the last element of the vector and then modifies it by assigning a new value. Next, the program also defines how the push_back() function adds a new element at the end and how the pop_back() function removes the last element. After that, it traverses and prints all elements of the vector using a loop. Finally, it checks if a vector is empty before using the back() function to avoid errors, which ensures safe usage.

Conclusion

In conclusion, the vector::back() function is used to access the last element of a vector. It enhances the code readability and performance by providing constant-time access. However, we must ensure that the vector is not empty before using it to avoid undefined behavior. Overall, the back() function is very useful when working with vectors, especially where the last element is frequently accessed or modified.