CPP STL Tutorial: C++ std::deque and it’s operations.

In this chapter we shall learn about:

1. deque introduction.
2. deque declaration.
3. Multidimensional std::deque
4. Passing std::deque to function
5. deque member function to iterate over elements.
6. deque member function to check the capacity.
7. deque member function to access the elements.
8. deque member function to modify the elements.
9. what is the advantage of deque over vector?
10. Do deque store in continuous memory or non continuous memory?

1. deque introduction.

deque is a double ended queue. You can insert elements at the beginning and also at the end.
The elements are stored in non continuous memory location.

Below header file is used for deque:

#include <deque>

2. deque declaration.

Below is how you declare a deque:

std::deque<int> mydeque;

3. Multidimensional std::deque

Below is how you declare a 2D deque:

deque<deque<int>> num = {
    { 1, 2, 3 },
    { 4, 5, 6 },
    { 7, 8, 9 }
};

4. Passing std::deque to function

You can pass a deque in 2 ways:

4.1. Pass by value
4.2. Pass by reference

4.1. Pass by value

void func(deque<int> deq) 
{ 
   deq.push_back(30); 
} 
   
int main() 
{ 
    deque<int> deq; 
    deq.push_back(10); 
    deq.push_back(20); 
   
    func(deq); 
   
    // as we are passing by value, the deque will remain unchanged.
    for (int i=0; i<deq.size(); i++) 
       cout << deq[i] << " "; 
   
    return 0; 
} 

4.2. Pass by reference

void func(deque<int> &deq) 
{ 
   deq.push_back(30); 
} 
   
int main() 
{ 
    deque<int> deq; 
    deque.push_back(10); 
    deq.push_back(20); 
   
    func(deq); 
   
    // as we are passing by reference, the deque will be changed.
    for (int i=0; i<deq.size(); i++) 
       cout << deq[i] << " "; 
   
    return 0; 
} 

5. deque member function to iterate over elements.

begin : It will return iterator to beginning
end   : It will return iterator to end
rbegin : It will return reverse iterator to reverse beginning
rend : It will return reverse iterator to reverse end
cbegin : It will return const_iterator to beginning  [C++ 11]
cend : It will return const_iterator to end [C++ 11]
#include <iostream>
#include <deque>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main(void)
{
    std::deque<int> mydeque;
    for (int i=1; i<=5; i++) 
        mydeque.push_back(i);

  std::cout << "mydeque contains:"<<endl;
  
  //iterate throughout the array, start from .begin till .end
  cout<<"Iterate using .begin and .end"<<endl;
  for (std::deque<int>::iterator it = mydeque.begin() ; it != mydeque.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';
  
  cout<<"Iterate using .cbegin and .cend"<<endl;
 for (auto it = mydeque.cbegin(); it != mydeque.cend(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  cout<<"Iterate using .rbegin and .rend"<<endl;
 for (auto it = mydeque.rbegin(); it != mydeque.rend(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';
    
}
Output:
mydeque contains:
Iterate using .begin and .end
 1 2 3 4 5
Iterate using .cbegin and .cend
 1 2 3 4 5
Iterate using .rbegin and .rend
 5 4 3 2 1

6. deque member function to check the capacity.

size : It will return size
max_size : It will return maximum size
resize : It will change size
empty : It will test if vector is empty
shrink_to_fit :It will shrink to fit [C++11]
#include <iostream>
#include <deque>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main(void)
{
    std::deque<int> mydeque;
    for (int i=1; i<=5; i++) 
        mydeque.push_back(i);

    cout<<"Size of deque using .size is "<<mydeque.size()<<endl;
    cout<<"Size of deque using .max_size is "<<mydeque.max_size()<<endl;

}
Output:
Size of deque using .size is 5
Size of deque using .max_size is 4611686018427387903

7. deque member function to access the elements.

operator[] : Access element
at : Access element
front : Access first element
back : Access last element
#include <iostream>
#include <deque>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main(void)
{
    std::deque<int> mydeque;
    for (int i=1; i<=5; i++) 
        mydeque.push_back(i);

    std::deque<int>::size_type sz = mydeque.size();
    std::cout << "mydeque contains:"<<endl;
    cout<<"Printing using operator[]"<<endl;
    for (unsigned i=0; i<sz; i++)
        std::cout << ' ' << mydeque[i];
    std::cout << '\n';
   
    cout<<"Printing using at()"<<endl;
    for (unsigned i=0; i<sz; i++)
        std::cout << ' ' << mydeque.at(i);
    std::cout << '\n'; 
    
    cout<<"The front of the deque using .front() = "<<mydeque.front()<<endl;
    cout<<"The front of the deque using .back() = "<<mydeque.back();
}
Output:
mydeque contains:
Printing using operator[]
 1 2 3 4 5
Printing using at()
 1 2 3 4 5
The front of the deque using .front() = 1
The front of the deque using .back() = 5

8. deque member function to modify the elements.

push_back : Add element at the end
push_front : Insert element at beginning
pop_back : Delete last element
pop_front : Delete first element
#include <iostream>
#include <deque>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main(void)
{
    std::deque<int> mydeque;
    for (int i=1; i<=5; i++) 
        mydeque.push_back(i);

    std::deque<int>::size_type sz = mydeque.size();
    std::cout << "mydeque contains:"<<endl;
    cout<<"Printing using operator[]"<<endl;
    for (unsigned i=0; i<sz; i++)
        std::cout << ' ' << mydeque[i];
    std::cout << '\n';
   

    mydeque.pop_front();
    cout<<"The front of the deque after pop_front using .front() = "<<mydeque.front()<<endl;
    
    mydeque.pop_back();
    cout<<"The back of the deque after pop_back using .back() = "<<mydeque.back();
}

Output:

mydeque contains:
Printing using operator[]
1 2 3 4 5
The front of the deque after pop_front using .front() = 2
The back of the deque after pop_back using .back() = 4

9. what is the advantage of deque over vector?

Below are the points where deque are efficient over vector:

1. They are not stored in continuous memory. Hence the allocation of deque can be scattered in different memory locations.
2. Vectors use single array, and usually reallocated when it grows.

10. Do deque store in continuous memory or non continuous memory?

non continuous memory location.

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *