C++ .push_front()

cheetah3051's avatar
Published Oct 14, 2025
Contribute to Docs

In C++, the .push_front() method adds an element to the beginning of the deque.

  • 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

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 the dequeName holds.

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 integers
std::deque<int> numbers;
// Use .push_front() to add elements to the beginning of the deque
numbers.push_front(30);
numbers.push_front(20);
numbers.push_front(10);
// 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 values to myDeque with the .push_front() method:

Code
Output
Loading...

All contributors

Contribute to 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