C++ std::deque at() Function

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

Definition

In C++, std::deque::at() gives you the element stored at one index of a std::deque. 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 <deque> to use it.

This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.

Syntax

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

C++
d.at(2);
d.at(2) = 50;
CallWhat it does
d.at(2)Reads the element at index 2. The index is checked first.
d.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 deque the reference is read-only.
ExceptionThrows std::out_of_range when pos >= size().

C++ Examples

Example 1 — Basic at()

Read elements by index, then see how push_front() moves the indexes.

C++
#include <deque>
#include <iostream>

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

    d.push_front(10);

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

How It Works

  1. The deque starts as 20 30 40, with valid indexes 0 to 2.
  2. push_front(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 <deque>
#include <iostream>
#include <stdexcept>

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

    try {
        std::cout << d.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 deque has only 3 elements, so the valid indexes are 0, 1, and 2.
  2. d.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 <deque>
#include <iostream>

int main() {
    std::deque<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::deque<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 deque.

Frequently Asked Questions

It returns the element stored at a given index of a std::deque. 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. d.at(i) and d[i] take constant time, like a vector. A deque keeps its elements in several blocks, so it is a little slower than a vector, but still fast.
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 deque can also grow at the front, which a vector cannot do quickly.

Next: back()

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

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.

9 people found this page helpful