public member function
<deque>

std::deque::at

      reference at (size_type n);
const_reference at (size_type n) const;
Access element
Returns a reference to the element at position n in the deque container object.

The function automatically checks whether n is within the bounds of valid elements in the container, throwing an out_of_range exception if it is not (i.e., if n is greater or equal than its size). This is in contrast with member operator[], that does not check against bounds.

Returns a reference to the element at position n in the deque container object.

The difference between this member function and member operator function operator[] is that deque::at signals if the requested position is out of range by throwing an out_of_range exception.

Parameters

n
Position of an element in the container.
If this is greater than the deque size, an exception of type out_of_range is thrown.
Notice that the first element has a position of 0 (not 1).
Member type size_type is an unsigned integral type.

Return value

The element at the specified position in the container.

If the deque object is const-qualified, the function returns a const_reference. Otherwise, it returns a reference.

Member types reference and const_reference are the reference types to the elements of the container (see deque member types).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// deque::at
#include <iostream>
#include <deque>

int main ()
{
  std::deque<unsigned> mydeque (10);   // 10 zero-initialized unsigneds

  // assign some values:
  for (unsigned i=0; i<mydeque.size(); i++)
    mydeque.at(i)=i;

  std::cout << "mydeque contains:";
  for (unsigned i=0; i<mydeque.size(); i++)
    std::cout << ' ' << mydeque.at(i);

  std::cout << '\n';

  return 0;
}


Output:
mydeque contains: 0 1 2 3 4 5 6 7 8 9

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed (neither the const nor the non-const versions modify the container).
Element n is potentially accessed or modified by the caller. Concurrently accessing or modifying other elements is safe.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the container.
It throws out_of_range if n is out of bounds.

See also