|
| 1 | +--- |
| 2 | +Title: '.at()' |
| 3 | +Description: 'Accesses an element at a specified index in a deque with bounds checking.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Game Development' |
| 7 | +Tags: |
| 8 | + - 'Containers' |
| 9 | + - 'Deques' |
| 10 | + - 'Methods' |
| 11 | + - 'STL' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The C++ **`.at()`** [function](https://www.codecademy.com/resources/docs/cpp/functions) accesses the element at a given index in a deque while performing bounds checking. If the index is out of range, the function throws an `std::out_of_range` [exception](https://www.codecademy.com/resources/docs/cpp/exceptions). This makes it a safer alternative to the subscript operator `[]`, which offers no safety checks. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +deque.at(pos) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `pos`: A zero-based index of the element to access. Must be within the valid range of the deque. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +Returns a reference to the element at the given position. |
| 32 | + |
| 33 | +**Exceptions:** |
| 34 | + |
| 35 | +Throws `std::out_of_range` if `pos` is invalid. |
| 36 | + |
| 37 | +## Example 1: Accessing Elements Safely |
| 38 | + |
| 39 | +In this example, `.at()` retrieves elements from valid positions in the deque: |
| 40 | + |
| 41 | +```cpp |
| 42 | +#include <iostream> |
| 43 | +#include <deque> |
| 44 | +using namespace std; |
| 45 | + |
| 46 | +int main() { |
| 47 | + deque<int> numbers = {10, 20, 30, 40}; |
| 48 | + |
| 49 | + cout << "Element at index 2: " << numbers.at(2) << endl; |
| 50 | + cout << "Element at index 0: " << numbers.at(0) << endl; |
| 51 | + |
| 52 | + return 0; |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +The output of this code is: |
| 57 | + |
| 58 | +```shell |
| 59 | +Element at index 2: 30 |
| 60 | +Element at index 0: 10 |
| 61 | +``` |
| 62 | + |
| 63 | +The `.at()` function returns elements at positions 2 and 0 with built-in bounds checking. |
| 64 | + |
| 65 | +## Example 2: Handling Out-of-Range Access |
| 66 | + |
| 67 | +In this example, accessing index 5 triggers an exception because the index is outside the deque's valid range: |
| 68 | + |
| 69 | +```cpp |
| 70 | +#include <iostream> |
| 71 | +#include <deque> |
| 72 | +using namespace std; |
| 73 | + |
| 74 | +int main() { |
| 75 | + deque<string> names = {"Ava", "Mira", "Leo"}; |
| 76 | + |
| 77 | + try { |
| 78 | + cout << names.at(5) << endl; |
| 79 | + } catch (const out_of_range& e) { |
| 80 | + cout << "Error: " << e.what() << endl; |
| 81 | + } |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +The output of this code is: |
| 88 | + |
| 89 | +```shell |
| 90 | +Error: deque::_M_range_check: __n (which is 5)>= this->size() (which is 3) |
| 91 | +``` |
| 92 | + |
| 93 | +## Codebyte Example |
| 94 | + |
| 95 | +This example demonstrates safe element access and shows how `.at()` behaves when an invalid index is used: |
| 96 | + |
| 97 | +```codebyte/cpp |
| 98 | +#include <iostream> |
| 99 | +#include <deque> |
| 100 | +using namespace std; |
| 101 | +
|
| 102 | +int main() { |
| 103 | + deque<char> letters = {'A', 'B', 'C', 'D'}; |
| 104 | +
|
| 105 | + cout << "First element: " << letters.at(0) << endl; |
| 106 | + cout << "Third element: " << letters.at(2) << endl; |
| 107 | +
|
| 108 | + try { |
| 109 | + cout << letters.at(10) << endl; |
| 110 | + } catch (const out_of_range& e) { |
| 111 | + cout << "Caught exception: " << e.what() << endl; |
| 112 | + } |
| 113 | +
|
| 114 | + return 0; |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +## Frequently Asked Questions |
| 119 | + |
| 120 | +### 1. What is the use of deque in C++? |
| 121 | + |
| 122 | +A deque (double-ended queue) stores elements in a dynamic sequence where insertions and deletions at both the front and back are efficient. It supports random access like a vector but provides faster operations at the beginning of the structure. |
| 123 | + |
| 124 | +### 2. What is a std::deque? |
| 125 | + |
| 126 | +`std::deque` is a standard container that provides a dynamic array-like structure with fast operations at both ends. It is implemented as segmented memory blocks, which allows it to grow without relocating all elements like a vector. |
| 127 | + |
| 128 | +### 3. How to check if deque is empty in C++? |
| 129 | + |
| 130 | +A deque can be checked for emptiness using the `.empty()` method, which returns `true` if it contains no elements and `false` otherwise. This check is constant time and works for all standard containers. |
0 commit comments