Skip to main content

C++ STL Deque Container – std::deque

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

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and

Top Tips For PCB Design Layout

Are you thinking about designing a printed circuit board? PCBs are quite complicated, and you need to make sure that the layout that you choose is going to operate as well as you want it to. For this reason, we have put together some top tips for PCB design layout. Keep reading if you would like to find out more about this. Leave Enough Space One of the most important design tips for PCB layout is that you need to make sure that you are leaving enough space between the components. While many people might think that packing components closely is the best route to take, this can cause problems further down the line. This is why we suggest leaving extra space for the wires that will spread. This way, you’ll have the perfect PCB design layout. Print Out Your Layout Struggling to find out if your components sizes match? Our next tip is to print out your layout and compare the printed version to your actual components. Datasheets can sometimes come with errors, so it doesn’t hurt to do