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

In C++, the std::vector class provides a member function at(), which is used to access elements in a vector. Unlike the [] operator, at() performs bounds checking, throwing an exception if the accessed index position is out of range. This makes at() a safer alternative for element access, especially in scenarios where the index might be dynamically calculated or uncertain.

Syntax of vector::at() Function

In C++, the vector::at() function has the following syntax:

T& at(size_type n);
const T& at(size_type n) const;

Note that there are two versions of this function: one for non-constant vectors and one for constant vectors. The latter is used when you have a const reference to the vector.

Parameters of vector::at()

The at() function takes a single parameter:

  • n: The position of the element in the vector to access. This is of type size_type, which is typically an unsigned integral type.

Return Value of vector::at()

The vector::at() function returns a reference to the element at the specified position in the vector. If the function is called on a const vector, it returns a const reference. If the specified position is out of the range of the vector, the function throws an std::out_of_range exception.

Examples of Using vector::at()

Example 1: Accessing Elements in a Non-Const Vector

Suppose we have a vector of integers like this,

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

Now, to access an element at index ‘i,’ we need to pass it to the ‘at()’ function like this:

numbers.at(i)

It will return a reference of a value at index i, that is (i-1)th element of vector. In C++, indexing starts from 0, so the index position of the first element of the vector is 0, and the index position of the second element is 1. Similarly, the index position of the nth element is n-1.

Whereas, if we try to access an element at an index that is invalid, basically, if that index does not exist in the vector, then the ‘at()’ function will raise an exception.

Let’s see the complete example,

#include <iostream>
#include <vector>

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

    try {
        // Accessing the third element
        std::cout << "Third element: " << numbers.at(2) << std::endl;

        // Accessing an element out of range
        std::cout << "Fifth element: " << numbers.at(22) << std::endl;
    }
    catch (std::out_of_range const& exc)
    {
        std::cerr << "Out of Range error: " << exc.what() << std::endl;
    }

    return 0;
}

Output

Third element: 33
Fifth element:
Out of Range error: vector::_M_range_check: __n (which is 22) >= this->size() (which is 5)

In this case, the vector had only 5 elements, and when we attempted to access the element at index 22, which is an invalid index position, the ‘at()’ function raised an ‘out_of_range’ exception.

Example 2: Chaging a value in vector using at() function

The ‘at()’ member function of a vector returns a reference to an element at a given index position. So, we can use this reference to modify the value at that index position in the vector. Let’s see an example where we will try to access and change the value at the 2nd position in the vector.

Let’s see the complete example,

#include <iostream>
#include <vector>

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

    // Access & Change 2nd element in vector i.e. at index 1
    numbers.at(1) = 999;

    for(auto& elem: numbers) {
        std::cout<< elem << "  ";
    }
    std::cout<< std::endl;

    return 0;
}

Output

11  999  33  44  55

We changed the second value of vector to 999 using at() function.

Summary

Today, we learned about the at() 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.