C++ .front()
Anonymous contributor
Published Oct 30, 2025
Contribute to Docs
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).
- For a non-const deque, returns
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 integersstd::deque<int> numbers;// Add some elements to the dequenumbers.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 elementnumbers.front() = 50;// Display updated deque contentsstd::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: 100Updated 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:
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
- 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