deque_insert( ) in C++ in STL

Given is the task to show the functionality of Deque insert( ) function in C++ STL
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.
The deque insert( ) function is used to insert the elements in the deque.
The function is use to insert the element at specified position.
The function is use to insert the element at specified position.
The function is also use to insert the n No. of element in deque.
The function is also use to insert the n No. of element in deque.
It is also use insert the elements in the range at the specified.
It is also use insert the elements in the range at the specified.
deque_name.insert (iterator position, const_value_type& value)
deque_name.insert (iterator position, size_type n, const_value_type& value)
deque_name.insert (iterator position, iterator first, iterator last)
Value – specifies the new element to be inserted.
Value – specifies the new element to be inserted.
n – specifies the number of elements to insert.
n – specifies the number of elements to insert.
first, last – It specifies the iterator which specifying a range of elements to be inserted.
first, last – It specifies the iterator which specifying a range of elements to be inserted.
It returns the iterator that points to first of new inserted element.
Input Deque − 1 2 3 4 5
Output New Deque − 1 1 2 3 4 5
Input Deque − 11 12 13 14 15
Output New Deque − 11 12 12 12 13 14 15
First we declare the deque.
First we declare the deque.
Then we print the deque.
Then we print the deque.
Then we declare insert( ) function.
Then we declare insert( ) function.
By using above approach we can insert new element.
// C++ code to demonstrate the working of deque insert( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
   // declaring the deque
   Deque<int> deque = { 55, 84, 38, 66, 67 };
   // print the deque
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   // declaring insert( ) function
   x = deque.insert(x, 22);
   // printing deque after inserting new element
   cout<< “ New Deque:”;
   for( x = deque.begin( ); x != deque.end( ); ++x)
      cout<< “ “ <<*x;
   return 0;
}
If we run the above code then it will generate the following output
Input - Deque: 55 84 38 66 67
Output - New Deque: 22 55 84 38 66 67
// C++ code to demonstrate the working of deque insert( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
   deque<char> deque ={ ‘B’ , ‘L’ , ‘D’ };
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   deque.insert(x + 1, 2, ‘O’);
   // printing deque after inserting new element
   cout<< “ New Deque:”;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< “ “ <<*x;
   return 0;
}
If we run the above code then it will generate the following output
Input – Deque: B L D
Output – New Deque: B L O O D
// C++ code to demonstrate the working of deque insert( ) function
#include<iostream.h>
#include<deque.h>
#include<vector.h>
Using namespace std;
int main( ){
   deque<int> deque ={ 65, 54, 32, 98, 55 };
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   vector<int7gt; v(3, 19);
   deque.insert(x, v.begin( ), v.end( ) );
   // printing deque after inserting new element
   cout<< “ New Deque:”;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< “ “ <<*x;
   return 0;
}
If we run the above code then it will generate the following output
Input – Deque: 65 54 32 98 55
Output – New Deque: 65 19 19 19 65 54 32 98 55