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

  • 9281021542 2018-02-20
  • 632231541 2018-02-20
  • 9751491540 2018-02-20
  • 8333621539 2018-02-20
  • 995001538 2018-02-20
  • 6477441537 2018-02-20
  • 511981536 2018-02-19
  • 2072881535 2018-02-19
  • 6024461534 2018-02-19
  • 7594061533 2018-02-19
  • 9311341532 2018-02-19
  • 4928331531 2018-02-19
  • 38561530 2018-02-19
  • 7776611529 2018-02-19
  • 3038041528 2018-02-19
  • 9566341527 2018-02-19
  • 3936031526 2018-02-19
  • 9531111525 2018-02-19
  • 291211524 2018-02-19
  • 8962001523 2018-02-19