deque_resize( ) in C++ in STL

C++Server Side ProgrammingProgramming

Given is the task to show the functionality of deque resize( ) 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 resize( ) function

The deque resize( ) function is used to change the size of deque. If the size is greater than the current size, then new elements are inserted at the end of the deque. If the given size is smaller than the current size, then extra elements are removed.

Syntax

dequename.resize(n)

dequename.resize(n)

n: It defines the size of deque

Example

Input Current size − 5

Deque − 12 13 14 15 16

Output Size after resize − 7

New Deque − 11 12 13 14 15 16 17

Input Current size − 5

Deque − F O R C E

Output Size after resize − 4

New Deque − F O R C

Approach can be followed

  • First we declare the deque.

  • Then we check the size of deque.

  • Then we print the deque.

  • Then we define the resize( ) function

  • Then we print the new deque after resize.

By using above approach we can resize the deque.

Example

/ / C++ code to demonstrate the working of deque resize( ) function
#include <iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
   // initializing the deque
   Deque<int> deque = { 85, 87, 88, 89, 90 };
   cout<< “ Size of deque” << deque.size( )<< “\n”;
   // print the deque
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   // defining the resize( ) function
   deque.resize(7);
   // printing deque after resize
   cout<< “Deque after resize” << deque.size( ) <<”\n”;
   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 - Size of deque: 5
Deque: 85 87 88 89 90
Output - Deque after resize: 7
New Deque: 85 87 88 89 90 0 0

Example

/ / C++ code to demonstrate the working of deque resize( ) function
#include <iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
   / / initializing deque
   deque<int> deque ={ 14, 15, 16, 17, 18, 19, 20 };
   cout<< “ Size of deque” << deque.size( )<< “\n”;
   / / print the deque
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   / / defining the resize( ) function
   deque.resize(5);
   / / printing deque after resize
   cout<< “Deque after resize” << deque.size( ) <<”\n”;
   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: Size of deque: 7
Deque:14 15 16 17 18 19 20
Output: Deque after size: 5
New Deque: 14 15 16 17 18
raja
Published on 26-Feb-2020 11:00:28
Advertisements