Here you will learn about C++ STL Deque container i.e. std::deque and all functions applicable on it.
Note: Deque should be pronounced as “deck”.
It named because Double Ended Queue (DEQUE). Deques are come under sequence containers. These are double ended with features of expansion and contraction on both the ends. These are similar to vectors. But more efficient than vectors in case of insertion and deletion of elements not only at end but also at the beginning of the sequence. But here contiguous memory allocation may not be guaranteed.
Also Read: C++ STL Vector Container – std::vector
C++ STL Deque
To use deque we must include its header <deque> i.e. #include<deque>
Different Syntax for Declaring Deque
Creating an empty deque:
deque <int> dequeName;
Creating a deque with 10 empty elements:
deque <int> marks(10);
Creating a deque with 10 elements, each element have value 3:
deque <int> marks(10,3);
Array to deque:
int array [5] = {1, 2, 3, 4, 5}; deque <int> ranks(array,array+5);
Copying all deque elements 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
Inserting Elements Into Deque
push_back(element): This inserts the element at the end of the deque.
push_front(element): This function inserts the element at the front of the deque.
insert(): insert() function can be used in different ways.
- We can insert an element at particular position pointed by the iterator. For this we use two arguments. Those are (iterator, value to be inserted) respectively.
- We can insert an element, “n” no. of times at front of the deque. For this we use three arguments. Those are (iterator, number n, inserted value) respectively.
- We can insert array elements form particular index to another index. For this we use three arguments, (iterator, arrayStartIndex, arrayLastIndex);
assign(): assign (num, value), this inserts value into deque num times.
Example program to show different ways of inserting element 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
Deleting Elements form Deque
pop_back(): This will deletes the last element of the deque.
pop_front(): This will deletes the first element of the deque.
erase(): This function deletes the element which pointed by the iterator at particular position.
clear(): This function removes all elements from the deque.
Example program to show different ways of deleting an element form 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 increasing or decreasing the current size of the deque.
size(): Returns an integer that the number of elements present in the deque
Max_size(): Returns a system and architecture dependent value.
empty(): This is Boolean function, that returns true if deque is empty returns false if it’s not empty.
swap(): It swaps the all elements of deque1 to deque2. And all values of deque2 to deque1.
Example program to demonstrate above functions:
#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
Comment below if you have queries or found any information incorrect in above tutorial for C++ STL Deque.
The post C++ STL Deque Container – std::deque appeared first on The Crazy Programmer.
from The Crazy Programmer http://www.thecrazyprogrammer.com/2017/07/stl-deque.html
Comments
Post a Comment