C++ .push_front()
Published Oct 14, 2025
Contribute to Docs
In C++, the .push_front() method adds an element to the beginning of the deque.
Syntax
dequeName.push_front(value);
Parameters:
value: The element to be added to the front of the deque. It can be of any data type that thedequeNameholds.
Return value:
void (no value is returned).
Example
The example below showcases the use of the .push_front() method:
#include <iostream>#include <deque>int main() {// Create a deque of integersstd::deque<int> numbers;// Use .push_front() to add elements to the beginning of the dequenumbers.push_front(30);numbers.push_front(20);numbers.push_front(10);// 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 values to myDeque with the .push_front() method:
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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours