Deque::front() and deque::back() 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:

Examples:

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++:

Output:

8

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:

Examples:

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++:

Output:

4

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.

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

  1. Add integers to the deque utilizing the push_back()
  2. Compare the first and final element.
  3. If the initial element is greater, subtract the final element from it in order to print it.
  4. 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++:

Output:

7

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.

2. 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.

3. 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.






Latest Courses