In this article, we will learn about back() member function of Vector in C++.

In C++, the vector class provides a member function back() to access the last element of vector. This function provides a reference to the last element in the vector, which is handy when you need to inspect or modify the final element of the vector, such as in stack-like operations where the most recently added element is of interest.

Syntax of vector::back()

The vector::back() function is provided in two versions:

reference back();
const_reference back() const;

The const_reference back() const version of this function will be invoked when someone calls back() function on a const vector object. Whereas, reference back() version of this function will be invoked when someone calls back() function on a non-const vector object.

Parameters of vector::back()

The vector::back() function does not accept any parameters. It operates on the vector instance it is called on.

Return Value of vector::back()

  • Non-const version: Returns a reference (reference) to the last element in the vector.
  • Const version: Returns a constant reference (const_reference) to the last element in the vector. This version is used when the vector object is const-qualified and the returned element cannot be modified.

It is crucial to remember that calling back() on an empty vector leads to undefined behavior. Therefore, ensure the vector contains elements before invoking this function.

Examples of vector::back()

Example 1: Accessing the Last Element

Suppose you have a vector of integers, and you want to get the very last number in that vector. In C++, you can use the back() member function of vector, which is like asking for the last item in the vector. It’s a convenient way to access the last element without needing to count how many elements are in the vector.

Let’s see the complete example,

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> numbers = {11, 22, 33, 44};

    // Accessing the last element
    int lastElement = numbers.back();

    std::cout << "The last element is: " << lastElement << std::endl;

    return 0;
}

Output

The last element is: 44

In this example, back() is used to access the last element of the vector, which is then printed.

Example 2: Modifying the Last Element

Let’s see the complete example,

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> numbers = {11, 22, 33, 44};

    // Modifying the last element
    numbers.back() = 999;

    std::cout << "After modification, the last element is: " << numbers.back() << std::endl;

    return 0;
}

Output

After modification, the last element is: 999

Here, back() is utilized to directly modify the last element of the vector. The last element is changed from 44 to 999, demonstrating how back() returns a reference that can be used to alter the vector’s contents.

Summary

In this article, we learned how to access the last element of a vector using the back() member function of vector in C++.

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.