deque_clear( ) and deque_erase( ) in C++ in STL


Given is the task to show the functionality of deque clear( ) and deque erase( ) function in C++ STL.

What is Deque

Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allow user to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended queue the insertion and deletion of data is possible at both the ends.

What is deque.clear( )

This function is used to remove all the elements of the deque and thus making its size 0.

Syntax

dequename.clear( )

dequename.clear( )

Input Deque − 96 97 98 100

Output Deque − empty

Input Deque &mijnus; 1 2 3 4 5 6

Output Deque − empty

Approach can be followed

  • First we declare the deque.

  • Then we print the deque.

  • Then we define the clear( ) function.

By using above approach we can clear all the deque.

Example

// C++ code to demonstrate the working of deque.clear( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
   // initializing the deque
   Deque<int> deque = { 85, 87, 88, 89, 90 };
   // print the deque
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   // defining clear( ) function
   deque.clear( );
   // printing new deque
   cout<< “ New Deque:”;
   for( x = deque.begin( ) ; x != deque.end( ); ++x)
      cout<< “ “ <<*x;
   return 0;
}

Output

If we run the above code then it will generate the following output

Input - Deque: 85 87 88 89 90
Output - New Deque: No Output

Updated on: 26-Feb-2020

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements