Dequefront And Dequeback In C++ - C++ Programming Tutorial
C++ Course / STL Queue & Stack / Dequefront And Dequeback In C++

Dequefront And Dequeback In C++

BLUF: Mastering Dequefront And Dequeback In C++ 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: Dequefront And Dequeback In C++

C++ is renowned for its efficiency. Learn how Dequefront And Dequeback In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, we will explore the deque::front and deque::back functions in C++ along with an illustrative example.

Double-ended queues, also known as deques, are data structures that can grow and shrink at both ends. While similar to vectors, deques excel at adding and removing elements at the front and back. Unlike vectors, deques do not guarantee contiguous memory allocation.

What is deque::front?

The front function pertains to the first element of the deque data structure container. This method is beneficial for accessing the initial element within a deque. It is a component of the standard template library (STL) in C++. This functionality is included in the deque headers file of the STL.

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

If the deque container is devoid of elements, it will trigger an undefined behavior.

In contrast, when the deque structure is not empty, it guarantees the prevention of any exceptions from being raised.

Example:

Let's consider a scenario to demonstrate the utilization of the dequeue::front method 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 function is employed to access the last element of the deque container. It retrieves the element positioned at the rear end of the deque. This function is beneficial for retrieving the initial element of a deque. It is a function from the standard template library (STL) that is commonly used in C++. The mentioned function is included in the <deque> header file. It provides the final element as a reference. When back is called on an empty deque, it produces undefined outcomes.

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

If the deque container is devoid of elements, it leads to undefined behavior.

In the scenario where the deque structure is not empty, it guarantees that no errors will be thrown.

Example:

Let's consider an example to demonstrate the utilization of the deque::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:

Initialize an empty deque of integers. Proceed by inserting integers into the deque and calculate the variance between the initial and ultimate elements.

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 consider a scenario to demonstrate the usage of deque::front and deque::back methods 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:

Accessing both the initial and final elements is a primary function of deque as a double-ended queue.

The front and back member functions of std::deque are employed to access the first and last elements of the deque. These methods are commonly used when handling elements at the boundaries of the deque.

  1. Efficient Adding and Removing at Both Ends:

The benefit of using a deque instead of a std::vector lies in its ability to efficiently add and delete elements at the front and back ends of the deque. This feature is particularly useful when managing a group of items that require frequent insertions or deletions at either end.

  1. Deque Explained as a Double-Ended Queue:

The std::deque serves as a double-ended queue, facilitating the addition and removal of elements at both ends. To access the first and last elements, utilize front and back, respectively. Deques are well-suited for algorithms that require manipulation at both ends of the sequence.

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