Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Deque of Pairs in C++ with Examples

  • Last Updated : 30 Dec, 2021

What is a deque?

In C++, the deque is a sequence container and it is also known by the name, double-ended queue. As the name implies, a deque allows insertion and deletion from both ends. Although a deque is similar to a vector, deques are more efficient compared to vectors. In vectors, contiguous storage allocation is guaranteed but this might not be the case with deques.  Deque is the special case of a queue as the insertion and deletion operations are allowed at both ends.

Functions associated with a deque:

  • push_front(): Used to push elements in the container from the front.
  • push_back(): Used to push elements in the container from the back.
  • front(): Used to refer to the first element of the container.
  • back(): Used to refer to the last element of the container.

What is pair?

Utility header in C++ provides us pair container. A pair consists of two data elements or objects.

  • The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second).
  • Pair is used to combine together two values that may be different in type. Pair provides a way to store two heterogeneous objects as a single unit.
  • Pair can be assigned, copied, and compared. The array of objects allocated in a map or hash_map is of type ‘pair’ by default in which all the ‘first’ elements are unique keys associated with their ‘second’ value objects.

To access the elements, we use variable name followed by dot operator followed by the keyword first or second.

How to access a pair?

The elements of a pair can be accessed by using the dot (.) operator.

Syntax:

auto fistElement = myPair.first;

auto fistElement = myPair.second;

This article focuses upon creating a deque of pairs.

Deque of pairs

Deque of pairs is a deque container in which each element is a pair on its own. 

Syntax:

deque<pair<dataType1, dataType2>> myContainer;

Here,

dataType1 and dataType2 can be either similar or dissimilar data types

Example 1: Below is the C++ program to implement the deque of pairs.

C++




// C++ program to demonstrate
// the working of deque
// of pairs
#include <bits/stdc++.h>
using namespace std;
  
// Function to print deque elements
void print(deque<pair<int
           bool> >& myContainer)
{
    for (auto currentpair : myContainer) 
    {
        // Each element of the deque is
        // a pair itself
        pair<int, bool> pr = currentpair;
  
        cout << "[ ";
  
        // Printing pair elements
        cout << pr.first << ' ' << 
                pr.second;
        cout << ']';
        cout << '\n';
    }
}
  
// Driver code
int main()
{
    // Declaring a deque of pairs
    // of type {int, bool}
    deque<pair<int, bool> >
          myContainer;
  
    // Declaring a pair
    pair<int, bool> pair1;
  
    // Initializing the
    // pair
    pair1 = make_pair(22, false);
  
    // Push the pair at the front
    // in the deque
    myContainer.push_front(pair1);
  
    // Declaring another pair
    pair<int, bool> pair2;
  
    // Initializing the
    // pair
    pair2 = make_pair(33, true);
  
    // Push the pair at the back
    // in the deque
    myContainer.push_back(pair2);
  
    // Declaring another pair
    pair<int, bool> pair3;
  
    // Initializing the pair
    pair3 = make_pair(11, false);
  
    // Push the pair at the front
    // in the deque
    myContainer.push_front(pair3);
  
    // Declaring another pair
    pair<int, bool> pair4;
  
    // Initializing the pair
    pair4 = make_pair(44, true);
  
    // Push the pair at the back
    // in the deque
    myContainer.push_back(pair4);
  
    // Calling print function
    print(myContainer);
    return 0;
}
Output

[ 11 0]
[ 22 0]
[ 33 1]
[ 44 1]

Example 2: Below is the C++ program to implement the deque of pairs.

C++




// C++ program to demonstrate
// the working of deque
// of pairs
#include <bits/stdc++.h>
using namespace std;
  
// Function to print deque elements
void print(deque<pair<string, 
           bool> >& myContainer)
{
    for (auto currentpair : myContainer) 
    {
        // Each element of the deque is
        // a pair itself
        pair<string, bool> pr = currentpair;
  
        cout << "[ ";
  
        // Printing pair elements
        cout << pr.first << ' ' << 
                pr.second;
        cout << ']';
        cout << '\n';
    }
}
  
// Driver code
int main()
{
    // Declaring a deque of pairs
    // of type {string, bool}
    deque<pair<string, bool> >
          myContainer;
  
    // Declaring a pair
    pair<string, bool> pair1;
  
    // Initializing the
    // pair
    pair1 = make_pair("GeeksforGeeks"
                       false);
  
    // Push the pair at the front
    // in the deque
    myContainer.push_front(pair1);
  
    // Declaring another pair
    pair<string, bool> pair2;
  
    // Initializing the
    // pair
    pair2 = make_pair("GFG", true);
  
    // Push the pair at the back
    // in the deque
    myContainer.push_back(pair2);
  
    // Declaring another pair
    pair<string, bool> pair3;
  
    // Initializing the pair
    pair3 = make_pair("Java"
                       false);
  
    // Push the pair at the front
    // in the deque
    myContainer.push_front(pair3);
  
    // Declaring another pair
    pair<string, bool> pair4;
  
    // Initializing the pair
    pair4 = make_pair("Python"
                       true);
  
    // Push the pair at the back
    // in the deque
    myContainer.push_back(pair4);
  
    // Calling print function
    print(myContainer);
    return 0;
}
Output

[ Java 0]
[ GeeksforGeeks 0]
[ GFG 1]
[ Python 1]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!