.push_back()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 14, 2024
Contribute to Docs

In C++, the .push_back() method adds an element to the end of the deque.

Syntax

dequeName.push_back(value);
  • value: The element to be added to the back of the deque. It can be of any data type that the dequeName holds.

Example

The example below showcases the use of the .push_back() method:

#include <iostream>
#include <deque>
int main() {
// Create a deque of integers
std::deque<int> numbers;
// Use .push_back() to add elements to the deque
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
// Display the elements of the deque
std::cout << "Deque contents: ";
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}

The above code generates the following output:

Deque contents: 10 20 30

Codebyte Example

The following codebyte adds several values to myDeque with the .push_back() method:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C++ on Codecademy