You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: 'Returns a reference to the last element in the deque.'
4
+
Subjects:
5
+
- 'Computer Science'
6
+
- 'Game Development'
7
+
Tags:
8
+
- 'Containers'
9
+
- 'Data Structures'
10
+
- 'Deques'
11
+
- 'Methods'
12
+
CatalogContent:
13
+
- 'learn-c-plus-plus'
14
+
- 'paths/computer-science'
15
+
---
16
+
17
+
In C++, the **`.back()`**[method](https://www.codecademy.com/resources/docs/cpp/methods) returns a reference to the last element in a [deque](https://www.codecademy.com/resources/docs/cpp/deque). It allows direct access to that element for reading or modifying without removing it.
18
+
19
+
> **Note:** Calling `.back()` on an empty deque leads to undefined behavior. Check with [`.empty()`](https://www.codecademy.com/resources/docs/cpp/deque/empty) before using `.back()`.
20
+
21
+
## Syntax
22
+
23
+
```pseudo
24
+
dequeName.back();
25
+
```
26
+
27
+
**Parameters:**
28
+
29
+
This method does not take any parameters.
30
+
31
+
**Return value:**
32
+
33
+
Returns a reference to the last element of the deque. If the deque is `const`, it returns a `const` reference.
34
+
35
+
## Example: Accessing and Updating the Last Element
36
+
37
+
This example retrieves the last number in a deque, updates it, and displays the change:
0 commit comments