C++ STL Deque Container – std::deque

Here you will find out about C++ STL Deque holder, for example, std:: deque and all capacities appropriate on it.

Note: Deque ought to be articulated as “deck”.

It named on the grounds that Double Ended Queue (DEQUE). Deques are gone under succession compartments.

These are twofold finished with highlights of extension and constriction on both the closures. These are like vectors.

Be that as it may, more productive than vectors if there should be an occurrence of inclusion and cancellation of components at the end as well as toward the start of the arrangement.

Be that as it may, here coterminous memory portion may not be ensured.

Likewise Read: C++ STL Vector Container – std::vector

C++ STL Deque

To utilize deque we should incorporate its header for example #include

Distinctive Syntax for Declaring Deque

Making a vacant deque:

deque <int> dequeName;

Making a deque with 10 void components:

deque <int> marks(10);

Making a deque with 10 components, every component have esteem 3:

deque <int> marks(10,3);

Exhibit to deque:

int array [5] = {1, 2, 3, 4, 5};
deque <int> ranks(array,array+5);

int exhibit [5] = {1, 2, 3, 4, 5};

deque ranks(array,array+5);

Replicating all deque components to another deque:

#include<iostream>
#include<deque>
 
using namespace std;
 
int main(){
		deque <int> deq1(5,10);
		deque <int> deq2(deq1);
		
		for(int i=0; i<5; i++)
			cout << deq1[i] << " " ;
		
		cout << endl;
		
		for(int i=0; i<5; i++)
			cout << deq2[i] << " " ;
		
		return 0;
}

Output

10 10 10 10 10
10 10 10 10 10

Embeddings Elements Into Deque

push_back(element): This embeds the component toward the finish of the deque.

push_front(element): This capacity embeds the component at the front of the deque.

embed(): embed() capacity can be utilized in various ways.

We can embed a component at a specific position pointed by the iterator. For this, we utilize two contentions.

Those are (iterator, worth to be embedded) individually.

We can embed a component, “n” no. of times at front of the deque. For this, we utilize three contentions. Those are (iterator, number n, embedded worth) separately.

We can embed exhibit components structure-specific list to another list. For this we utilize three contentions, (iterator, arrayStartIndex, arrayLastIndex);

relegate(): dole out (num, esteem), these additions esteem into deque num times.

Model program to show various methods for embeddings component into deque:

#include<iostream>
#include<deque>
 
using namespace std;
 
int main(){
	int element;
	deque <int> dek;
	
	cout << "enter an element to insert at back" << endl;
	cin >> element;
	dek.push_back (element);
	
	cout << "enter an element to insert at front" << endl;
	cin >> element;
	dek.push_front (element);
	
	deque <int> :: iterator it;
	it = dek.begin();
	
	// inserting at particluar posiion using insert()
	cout << "inserting element 15 at start of the deque using iterator" << endl;
	dek.insert(it, 15);
	
	// inserting element, 2 times at the end of the deque
	cout << "inserting element 10, two times at end of the deque" << endl;
	it = dek.end();
	dek.insert (it, 2, 10);
	
	// inserting first 3 elements of the array to front of the deque
	cout << "Inserting first 3 elemtns of the array(1,2,3) to at the fornt of the deque" << endl;
	it = dek.begin();
	int array[5] = { 1, 2, 3, 4, 5};
	dek.insert(it, array, array+3);
	
	cout << "firnal result is" << endl;
	for(int i=0; i<dek.size(); i++)
		cout << dek[i] << " ";
	
	// using assign() to insert elements
	cout << "using assign inserting into new deque" << endl;
	deque <int> newdq;
	newdq.assign(5,99);
	cout << "New deque elements are" << endl;
	
	for(int i=0; i<newdq.size(); i++)
		cout << newdq[i] << " ";
 	
 	return 0;
}

Output

enter an element to insert at back
10
enter an element to insert at front
15
inserting element 15 at start of the deque using iterator
inserting element 10, two times at end of the deque
Inserting first 3 elemtns of the array(1,2,3) to at the fornt of the deque
firnal result is
1 2 3 15 15 10 10 10 using assign inserting into new deque
New deque elements are
99 99 99 99 99

Erasing Elements structure Deque

pop_back(): This will erase the last component of the deque.

pop_front(): This will erase the primary component of the deque.

eradicate(): This capacity erases the component which pointed by the iterator at a specific position.

clear(): This capacity expels all components from the deque.

Model program to show various methods for erasing a component structure deque:

#include<iostream>
#include<deque>
 
using namespace std;
 
int main(){
	deque <int> dek;
	
	for(int i=0; i<7; i++)
	    dek.push_back(i);
	cout << "Initially deque contains elements are " << endl;
	
	for(int i=0; i<7; i++)
	    cout << dek[i] << " ";
	
	cout << endl;
	
	// uisng pop_back
	cout << "Deleting last element using pop_back" << endl;
	dek.pop_back();
	
	// using pop_front
	cout << "Deleting first element using pop_fornt" << endl;
	dek.pop_front();
	
	// deleting an element at particular position
	deque <int> :: iterator it;
	it = dek.begin();
	cout << "deleting element at index 2" << endl;
	dek.erase(it+2);
	
	cout << "Resultant deque upto now-> " <<  endl;
	for(int i=0; i<dek.size(); i++)
	    cout << dek[i] << " ";
	
	cout << endl;
	
	// clear functios.
	cout << "using clear function" << endl;
	dek.clear();
	
	// checking dek is empty or not.
	dek.empty() ? cout << "finally deque is empty" : cout << "deque is not empty";
 	
 	return 0;
}

Output

Initially deque contains elements are
0 1 2 3 4 5 6
Deleting last element using pop_back
Deleting first element using pop_fornt
deleting elements at index 2
Resultant deque upto now->
1 2 4 5
using clear function
finally deque is empty

resize(): Resize can be applied for expanding or diminishing the present size of the deque.

size(): Returns a whole number that the number of components present in the deque

Max_size(): Returns a framework and design subordinate worth.

void(): This is Boolean work, that profits genuinely if deque is unfilled returns bogus if it’s not void.

swap(): It swaps all components of deque1 to deque2. And all estimations of deque2 to deque1.

Model program to exhibit the above capacities:

#include<iostream>
#include<deque>
 
using namespace std;
 
int main(){
	deque <int> dek;
	
	for(int i=0; i<5; i++)
	    dek.push_back(i);
	
	cout << "deque size is " << dek.size() << endl;
	
	// resizing
	dek.resize(3);
	cout << "deque size after resize is " << dek.size() << endl;
	cout << "maxsize of deque is " << dek.max_size() << endl;
	
	//checking empty condition
	dek.empty() ? cout << "finally deque is empty" : cout << "deque is not empty";
	cout << endl;
	
	deque <int> d1(5,10);
	deque <int> d2(5,20);
	
	cout << "Elements of the deque1 before swap" << endl;
	for(int i=0; i<5; i++)
	    cout << d1[i] << " ";
	
	cout << endl;
	cout << "Elements of the deque2 before swap" << endl;
	
	for(int i=0; i<5; i++)
	    cout << d2[i] << " ";
	
	cout << endl;
	cout << "Elements of the deque1 after swap" << endl;
	d1.swap(d2);
	
	for(int i=0; i<5; i++)
	    cout << d1[i] << " ";
	
	cout << endl;
	cout << "Elements of the deque2 after swap" << endl;
	
	for(int i=0; i<5; i++)
	    cout << d2[i] << " ";
	
	cout << endl;
 	
 	return 0;
}

Output

deque size is 5
deque size after resize is 3
maxsize of deque is 4611686018427387903
deque is not empty
Elements of the deque1 before swap
10 10 10 10 10
Elements of the deque2 before swap
20 20 20 20 20
Elements of the deque1 after swap
20 20 20 20 20
Elements of the deque2 after swap
10 10 10 10 10

Remark underneath in the event that you have inquiries or discovered any data mistaken in above instructional exercise for C++ STL Deque.

Leave a Comment

error: Alert: Content is protected!!