English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Deque back() Usage and Example

C++ Deque (Double-ended Queue)

C ++ The Deque back() function is used to access the last element of the deque container.

Syntax

reference back();

Parameters

It does not contain any parameters.

Return value

It returns a reference to the last element of the deque container.

Example1

Let's look at a simple example

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

Output:

5

In this example, the back() function returns the last element of the deque, that is5.

Example2

Let's look at another simple 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 the deque: ";
  for(itr = n.begin(); itr != n.end();++itr)
  cout <<*itr << " \t ";
  return 0;
}

Output:

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

C++ Deque (Double-ended Queue)