Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ deque::at function is used to return a reference to the element of the deque at the specified position. If the specified position is not within the bounds of valid elements in the deque, the function throws an out_of_range exception.

Note: The deque::at function produces the same result as deque::operator[] function, except deque::operator[] does not check the specified position against bounds of valid elements in the deque.

Syntax

reference at (size_type n);
const_reference at (size_type n) const;
reference at (size_type n);
const_reference at (size_type n) const;

Parameters

n Specify position of the element in the deque.

Return Value

The element at the specified position in the deque.

Time Complexity

Constant i.e, Θ(1).

Example:

In the below example, the deque::at function is used to return a reference to the element of the deque called MyDeque at the specified positions.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> MyDeque{10, 20, 30};
  MyDeque.push_back(40);
  MyDeque.push_back(50);

  cout<<"The deque contains:";
  for(int i = 0; i < MyDeque.size(); i++)
    cout<<" "<<MyDeque.at(i);

  return 0;
}

The output of the above code will be:

The deque contains: 10 20 30 40 50

❮ C++ <deque> Library