Open In App

Deque::front() and deque::back() in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque.

deque::front()

front() is used to reference the first element of the deque container. This function can be used to fetch the first element of a deque. This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <deque> header file. 

Syntax : 

dequename.front()

Returns: Direct reference to the first element of the deque container.

Examples: 

Input  :  mydeque = 1, 2, 3
mydeque.front();
Output : 1
Input : mydeque = 3, 4, 1, 7, 3
mydeque.front();
Output : 3

Errors and Exceptions: 

  1. If the deque container is empty, it causes undefined behavior.
  2. It has a no exception throw guarantee if the deque is not empty.

C++




// CPP program to demonstrate
// application Of front() and back() function
#include <deque>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    deque<int> mydeque;
    mydeque.push_back(8);
    mydeque.push_back(7);
    mydeque.push_back(6);
    mydeque.push_back(5);
    mydeque.push_back(4);
    mydeque.push_back(3);
    mydeque.push_back(2);
    mydeque.push_back(1);
    // deque becomes 8, 7, 6, 5, 4, 3, 2, 1
 
    if (mydeque.front() > mydeque.back()) {
        cout << mydeque.front() - mydeque.back();
    }
    else if (mydeque.front() < mydeque.back()) {
        cout << mydeque.back() - mydeque.front();
    }
    else
        cout << "0";
 
    return 0;
}


Output

3

Time complexity: O(1)

Auxiliary Space: O(1)

deque::back()

back() function is used to reference the last element of the deque container. This function can be used to fetch the first element from the back of a deque. This function can be used to fetch the first element of a deque. This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <deque> header file. 

Syntax : 

dequename.back()

Returns: Direct reference to the last element of the deque container.

Examples:  

Input  :  mydeque = 1, 2, 3
mydeque.back();
Output : 3
Input : mydeque = 3, 4, 1, 7, 3
mydeque.back();
Output : 3

Errors and Exceptions: 

  1. If the deque container is empty, it causes undefined behavior.
  2. It has a no exception throw guarantee if the deque is not empty.

C++




<div id="highlighter_887467" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments">// CPP program to demonstrate</code></div><div class="line number2 index1 alt1"><code class="comments">// Implementation of back() function</code></div><div class="line number3 index2 alt2"><code class="preprocessor">#include <deque></code></div><div class="line number4 index3 alt1"><code class="preprocessor">#include <iostream></code></div><div class="line number5 index4 alt2"><code class="keyword bold">using</code> <code class="keyword bold">namespace</code> <code class="plain">std;</code></div><div class="line number6 index5 alt1"> </div><div class="line number7 index6 alt2"><code class="color1 bold">int</code> <code class="plain">main()</code></div><div class="line number8 index7 alt1"><code class="plain">{</code></div><div class="line number9 index8 alt2"><code class="undefined spaces">    </code><code class="plain">deque<</code><code class="color1 bold">int</code><code class="plain">> mydeque;</code></div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="plain">mydeque.push_back(3);</code></div><div class="line number11 index10 alt2"><code class="undefined spaces">    </code><code class="plain">mydeque.push_back(4);</code></div><div class="line number12 index11 alt1"><code class="undefined spaces">    </code><code class="plain">mydeque.push_back(1);</code></div><div class="line number13 index12 alt2"><code class="undefined spaces">    </code><code class="plain">mydeque.push_back(7);</code></div><div class="line number14 index13 alt1"><code class="undefined spaces">    </code><code class="plain">mydeque.push_back(3);</code></div><div class="line number15 index14 alt2"><code class="undefined spaces">    </code><code class="comments">// Queue becomes 3, 4, 1, 7, 3</code></div><div class="line number16 index15 alt1"> </div><div class="line number17 index16 alt2"><code class="undefined spaces">    </code><code class="plain">cout << mydeque.back();</code></div><div class="line number18 index17 alt1"><code class="undefined spaces">    </code><code class="keyword bold">return</code> <code class="plain">0;</code></div><div class="line number19 index18 alt2"><code class="plain">}</code></div></div></td></tr></tbody></table></div>


Output

3

Time complexity: O(1)

Auxiliary Space: O(1)

Application: deque::front() and deque::back()

Given an empty deque of integers, add numbers to the deque, then print the difference between the first and the last element.  

Input  : 1, 2, 3, 4, 5, 6, 7, 8
Output : 7

(Explanation: The last element is 8, the first element is 1, Difference will be 7)

Algorithm: 
1. Add numbers to the deque using the push_back() function. 
2. Compare the first and the last element. 
3. If the first element is larger, subtract the last element from it and print it. 
4. Else subtract the first element from the last element and print it.

C++




// CPP program to demonstrate
// application Of front() and back() function
#include <deque>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    deque<int> mydeque;
    mydeque.push_back(8);
    mydeque.push_back(7);
    mydeque.push_back(6);
    mydeque.push_back(5);
    mydeque.push_back(4);
    mydeque.push_back(3);
    mydeque.push_back(2);
    mydeque.push_back(1);
    // deque becomes 8, 7, 6, 5, 4, 3, 2, 1
 
    if (mydeque.front() > mydeque.back()) {
        cout << mydeque.front() - mydeque.back();
    }
    else if (mydeque.front() < mydeque.back()) {
        cout << mydeque.back() - mydeque.front();
    }
    else
        cout << "0";
 
    return 0;
}


Output

7

Time complexity: O(1)

Auxiliary Space: O(1)

Let us see the differences in a tabular form -:

  Deque::front() deque::back() 
1. It is used to return a reference to the first element in the deque container. It is used to return a reference to the last element in the container.
2.

Its syntax is -:

reference front();

Its syntax is -:

reference back();

3. It does not take any parameters. It does not take any parameters.
4. Its complexity is constant. Its complexity is constant.
5. Its iterator validity does not change. Its iterator validity does not change.

 



Last Updated : 11 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads