C++ .front()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 30, 2025
Contribute to Docs

In C++, the .front() method returns a reference to the first element in 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

deque_name.front()

Parameters:

.front() does not take any parameters

Return value:

  • Returns a reference to the first element of the deque.
    • For a non-const deque, returns T& (modifiable).
    • For a const deque, returns const T& (read-only).

Example

The example below demonstrates use of .front() to access the first element in a deque:

#include <iostream>
#include <deque>
int main() {
// Create a deque of integers
std::deque<int> numbers;
// Add some elements to the deque
numbers.push_back(100);
numbers.push_back(200);
numbers.push_back(300);
// Access the first element using .front()
std::cout << "First element: " << numbers.front() << std::endl;
// Modify the first element
numbers.front() = 50;
// Display updated deque contents
std::cout << "Updated deque: ";
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}

The output of this program will be:

First element: 100
Updated deque: 50 200 300

This shows that .front() allows both access and modification of the deque’s first element.

Codebyte Example

Run the following codebyte example to understand the use of .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