Deque Back Function - C++ Programming Tutorial
C++ Course / STL Queue & Stack / Deque Back Function

Deque Back Function

BLUF: Mastering Deque Back Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Deque Back Function

C++ is renowned for its efficiency. Learn how Deque Back Function enables low-level control and high-performance computing in the tutorial below.

C++ Deque back

The back method in C++ for deque is employed to retrieve the final element within the deque data structure.

Syntax

Example

reference back();

Parameter

It does not contain any parameter.

Return value

It provides a reference to the final element of the deque data structure.

Example 1

Let's see a simple example

Example

#include <iostream>
#include<deque>
using namespace std;
int main()
{
   deque<int> k={1,2,3,4,5};
   cout<<k.back();
   return 0;
}

Output:

In this instance, the back method retrieves the final element of the deque, which is 5.

Example 2

Let's see a another simple example

Example

#include <iostream>
#include<deque>
using namespace std;
int main()
{
  deque<int> n;
  deque<int>::iterator itr;
  n.push_back(15);
  while(n.back()!=0)
  {
      n.push_back(n.back()-1);
  } 
  cout<<"Content of deque:";
  for(itr=n.begin();itr!=n.end();++itr)
  cout<<*itr<<" ";
  return 0;
}

Output:

Output

Content of deque:15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience