C++ deque at() Function
Example
Get the second and the third element of a deque:
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars.at(1) << "\n";
cout << cars.at(2);
Definition and Usage
The at() function returns the element at a specified index. The first element has index 0, the second has index 1, and so on.
If the index is out of range, at() throws an out_of_range exception (an error). This makes it safer than square brackets [], which do not check the index.
The at function is defined in the <deque> header file.
Tip: You can also use at() to change an element, like cars.at(0) = "Opel";.
Syntax
deque.at(size_t index);
The size_t data type is a non-negative integer.
Parameter Values
| Parameter | Description |
|---|---|
| index | Required. The index (position) of the element. |
Technical Details
| Returns: | A reference to the element at the specified index. |
|---|
Related Pages
Read more about deques in our Deque Tutorial.