deque_insert( ) in C++ in STL

C++Server Side ProgrammingProgramming

Given is the task to show the functionality of Deque insert( ) 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 insert( )

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 also use to insert the n No. of element in deque.

  • It is also use insert the elements in the range at the specified.

Syntax

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)

Parameters

  • Value – specifies the new element to be inserted.

  • n – specifies the number of elements to insert.

  • first, last – It specifies the iterator which specifying a range of elements to be inserted.

Return Value

It returns the iterator that points to first of new inserted element.

Example

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

Approach can be followed

  • First we declare the deque.

  • Then we print the deque.

  • Then we declare insert( ) function.

By using above approach we can insert new element.

Example

// 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;
}

Output

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

Example

// 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;
}

Output

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

Example

// 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;
}

Output

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
raja
Published on 26-Feb-2020 12:22:12
Advertisements