.push_back()
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 thedequeName
holds.
Example
The example below showcases the use of the .push_back()
method:
#include <iostream>#include <deque>int main() {// Create a deque of integersstd::deque<int> numbers;// Use .push_back() to add elements to the dequenumbers.push_back(10);numbers.push_back(20);numbers.push_back(30);// Display the elements of the dequestd::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:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours