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: 'Adds an element to the end of the deque.'
4
+
Subjects:
5
+
- 'Computer Science'
6
+
- 'Game Development'
7
+
Tags:
8
+
- 'Containers'
9
+
- 'OOP'
10
+
- 'Classes'
11
+
- 'Deques'
12
+
CatalogContent:
13
+
- 'learn-c-plus-plus'
14
+
- 'paths/computer-science'
15
+
---
16
+
17
+
In C++, the **`.push_back()`**[method](https://www.codecademy.com/resources/docs/cpp/methods) adds an element to the end of the deque.
18
+
19
+
## Syntax
20
+
21
+
```pseudo
22
+
dequeName.push_back(value);
23
+
```
24
+
25
+
-`value`: The element to be added to the back of the deque. It can be of any [data type](https://www.codecademy.com/resources/docs/cpp/data-types) that the `dequeName` holds.
26
+
27
+
## Example
28
+
29
+
The example below showcases the use of the `.push_back()` method:
30
+
31
+
```cpp
32
+
#include<iostream>
33
+
#include<deque>
34
+
35
+
intmain() {
36
+
// Create a deque of integers
37
+
std::deque<int> numbers;
38
+
39
+
// Use .push_back() to add elements to the deque
40
+
numbers.push_back(10);
41
+
numbers.push_back(20);
42
+
numbers.push_back(30);
43
+
44
+
// Display the elements of the deque
45
+
std::cout << "Deque contents: ";
46
+
47
+
for (int num : numbers) {
48
+
std::cout << num << " ";
49
+
}
50
+
51
+
std::cout << std::endl;
52
+
53
+
return 0;
54
+
}
55
+
```
56
+
57
+
The above code generates the following output:
58
+
59
+
```shell
60
+
Deque contents: 10 20 30
61
+
```
62
+
63
+
## Codebyte Example
64
+
65
+
The following codebyte adds several values to `myDeque` with the `.push_back()` method:
0 commit comments