Skip to content

Commit 8e874bd

Browse files
authored
[Term Entry] C++ Deque: .push_back()
* Term Entry: C++ Deque Push_Back() * Update content/cpp/concepts/deque/terms/push_back/push_back.md Co-authored-by: Mamta Wardhani <mamta.wardhani@gmail.com> * Modify: modifications made in deque push_back term * Update package.json fixed package json file * Update yarn.lock fixed yarn lock * Update push_back.md minor changes * Rename push_back.md to push-back.md corrected file name * [Term Entry]: Added C++ insert under Deque * Delete content/cpp/concepts/deque/terms/insert/insert.md * Minor changes ---------
1 parent f0bbbe8 commit 8e874bd

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
Title: '.push_back()'
3+
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+
int main() {
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:
66+
67+
```codebyte/cpp
68+
#include <iostream>
69+
#include <deque>
70+
#include <string>
71+
72+
int main() {
73+
std::deque<std::string> myDeque;
74+
75+
myDeque.push_back("A");
76+
myDeque.push_back("B");
77+
myDeque.push_back("C");
78+
myDeque.push_back("D");
79+
80+
for (const auto& value : myDeque) {
81+
std::cout << ' ' << value;
82+
}
83+
}
84+
```

0 commit comments

Comments
 (0)