Dequefront And Dequeback In C++

In this article, we will discuss the deque::front and dequeuer::back in C++ with its example.

Deque , or Double Ended queues , are sequence containers that may expand and compress on both ends. They are comparable to vectors but more effective in terms of element insertion and deletion at their end and beginning. Contiguous memory allocation may not be ensured in the deque, unlike vectors.

What is deque::front?

The front refers to the initial element of the deque structure container. This function is useful for getting the initial element of a deque. It is a standard template library (STL) function used by C++. This function is part of the <deque> headers file.

Syntax:

It has the following syntax:

Example

dequename.front()

Examples:

Example

Input: myqueue=4,5,6
  myqueue.front();
Output: 4
Input: myqueue=6,9,0
  myqueue.front();
Output: 6

Exceptions and Errors:

  • If the deque structure container is empty, an unidentified response results.
  • If the deque structure is not empty, it ensures that no exceptions will be thrown.
  • Example:

Let's take an example to illustrate the use of dequqe::front function in C++:

Example

// program to implement the front() function using queues
#include <deque>
#include <iostream>
using namespace std;

int main()
{
	deque<int> myqueue;
	myqueue.push_back(8);
	myqueue.push_back(9);
	myqueue.push_back(7);
	myqueue.push_back(4);
	myqueue.push_back(2);
	// Queue becomes 8,9,7,4,2

	cout << myqueue.front();
	return 0;
}

Output:

Complexity:

Time Complexity: O(1)

Space Complexity: O(1)

What is deque::back?

The back method is used to refer to this deque container's final element. This method returns the first element from the rear of a deque. This method is useful for getting the initial element of a deque. It is a standard template library (STL) function used in C++. The above function is part of the <deque> header file. It returns the final element as a reference. If the deque structure is empty, invoking back returns undefined results.

Syntax:

It has the following syntax:

Example

deque.back()

Examples:

Example

Input: deque=4,5,6
  deque.back();
Output: 6
Input: deque=6,9,1
  deque.back();
Output: 1

Exceptions and Errors:

  • If the deque container is empty, undetermined behavior occurs.
  • If the deque structure is not empty, it ensures that no exceptions will be thrown.
  • Example:

Let's take an example to illustrate the use of dequqe::back function in C++:

Example

// program to implement the back() function using queues
#include <deque>
#include <iostream>
using namespace std;
int main()
{
	deque<int> myqueue;
	myqueue.push_back(8);
	myqueue.push_back(9);
	myqueue.push_back(7);
	myqueue.push_back(6);
	myqueue.push_back(4);
	// Queue becomes 8,9,7,6,4

	cout << myqueue.back();
	return 0;
}

Output:

Application using deque::front and deque::back methods:

Given an empty deque of integers, add integers to the deque and indicate the difference among the first and final members.

Example

Input: 1, 7,9,8,2,4
Output: 3

(The explanation: the last component is 4, while the initial element is 1. The difference is 3)

  • Add integers to the deque utilizing the push_back
  • Compare the first and final element.
  • If the initial element is greater, subtract the final element from it in order to print it.
  • If not, remove the first element with the final element and output the result.
  • Example:

Let's take an example to illustrate the use of dequqe::front and deque::back function in C++:

Example

// CPP program to demonstrate
// application Of front() and back() function
#include <deque>
#include <iostream>
using namespace std;

// Driver Code
int main()
{
	deque<int> que;
	que.push_back(9);
	que.push_back(5);
	que.push_back(5);
	que.push_back(2);
	que.push_back(8);
	que.push_back(7);
	que.push_back(2);
	que.push_back(2);
	// deque becomes 9,5,5,2,8,7,2,2
 // condition to check for positive difference
	if (que.front() > que.back()) {
		cout << que.front() - que.back();
	}
	else if (que.front() < que.back()) {
		cout << que.back() - que.front();
	}
	else
		cout << "0";

	return 0;
}

Output:

Uses of Deque as a Double-Ended Queue:

There are several uses of deque as double-ended queue. Some main uses of deque as double-ended queue are as follows:

  1. Accessing the First and Last members:

The std::deque member methods front and back are used to retrieve the deque's first and last members. These functions are typically utilized when working with items at the deque's boundaries.

  1. Reliable Insertion and Deletion on Both Ends:

The advantage of a deque over a std::vector is its capacity to insert and remove elements effectively at both the beginning and final stages of the deque. It is especially handy when we need to keep track of a collection of objects and often make insertions or removals at the front or rear.

  1. Deque as a Double-Ended Queue:

The std::deque can be implemented as a double-ended queue, enabling insertion and deletion at both ends. To get to the front and back components, use front and rear, accordingly. Dequeues are thus appropriate for algorithms that need deletions and insertions at both ends of the series.

Input Required

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