C++ std::vector at() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::vector

Definition

In C++, std::vector::at() gives you the element stored at one index of a std::vector. Counting starts at 0, so the first element is index 0 and the last one is index size() - 1.

The special thing about at() is that it checks the index. If the index is too big, it throws std::out_of_range instead of reading random memory. Include <vector> to use it.

This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.

Syntax

Call at on the vector you already have. Pass one index inside the brackets.

C++
v.at(2);
v.at(2) = 50;
CallWhat it does
v.at(2)Reads the element at index 2. The index is checked first.
v.at(2) = 50Changes the element at index 2. This works because at() returns a reference.

Square brackets also work: d[2]. The difference is that [] does not check the index.

Library declarations
C++
reference       at(size_type pos);
const_reference at(size_type pos) const;

Parameters

ParameterDescription
posRequired. The index of the element, counting from 0. It must be less than size().

Return Value

Return ValueDescription
ReferenceA reference to the element at index pos. You can read it or assign to it. For a const vector the reference is read-only.
ExceptionThrows std::out_of_range when pos >= size().

C++ Examples

Example 1 — Basic at()

Read elements by index, then insert at the front and see how the indexes move.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {20, 30, 40};

    v.insert(v.begin(), 10);

    std::cout << "v.at(0) = " << v.at(0) << '\n';
    std::cout << "v.at(3) = " << v.at(3) << '\n';
    return 0;
}

How It Works

  1. The vector starts as 20 30 40, with valid indexes 0 to 2.
  2. insert(v.begin(), 10) adds a new first element, so every old element moves one index higher: it is now 10 20 30 40.
  3. at(0) returns the new front value 10. at(3) returns 40, which used to sit at index 2.

Example 2 — Bad Index (out_of_range)

Use a try / catch block to handle a bad index safely.

C++
#include <vector>
#include <iostream>
#include <stdexcept>

int main() {
    std::vector<int> v = {1, 2, 3};

    try {
        std::cout << v.at(7) << '\n';
    } catch (const std::out_of_range& e) {
        std::cout << "Caught: index 7 is out of range\n";
    }
    return 0;
}

How It Works

  1. The vector has only 3 elements, so the valid indexes are 0, 1, and 2.
  2. v.at(7) checks the index first and throws std::out_of_range.
  3. The catch block runs, so the program prints a message instead of crashing. With d[7] there would be no check at all.

Example 3 — Change Values with at()

Because at() returns a reference, you can assign to it.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> scores(4, 0);

    scores.at(1) = 95;
    scores.at(3) = 88;

    for (std::size_t i = 0; i < scores.size(); ++i) {
        std::cout << "scores[" << i << "] = " << scores.at(i) << '\n';
    }
    return 0;
}

How It Works

  1. std::vector<int> scores(4, 0) creates four elements, all 0.
  2. scores.at(1) = 95 writes straight into index 1 because at() gives back a reference.
  3. The loop uses size() as its limit, so every at(i) call stays inside the vector.

Frequently Asked Questions

It returns the element stored at a given index of a std::vector. It checks the index first and throws std::out_of_range if the index is too large.
Both read an element by index. at() checks the index and throws std::out_of_range for a bad one. operator[] does no check, so a bad index is undefined behavior. operator[] can be slightly faster.
Yes. v.at(i) and v[i] take constant time because a vector stores elements in one contiguous block. That is one reason vector is the default sequence container.
The index type is size_type, which is unsigned. A negative number turns into a very large value, so at() throws std::out_of_range.
Yes. std::vector::at() works in the same way. The difference is that a vector can also grow at the front, which a vector cannot do quickly.

Next: back()

Get the last element of the vector with std::vector.

back() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful