Open In App

How to Compare Two Deques in C++?

In C++ the Standard Template Library (STL) provides a container called deque (short for double-ended queue) that allows fast insertions and deletions at both ends of the deque. In this article, we will learn how to compare two deques in C++.

Example:

Input: 
deque1 = {10,20,30}; 
deque2 = {10,20,30};

Output:  Both deques are equal

Compare Two Deques in C++

To compare two std::deques in C++, we can use the equal to (==) operator. This operator compares the corresponding elements of the two deques from the beginning to the end. If all pairs of elements are equal, then the deques are considered equal.

C++ Program to Compare Two Deques

The following program demonstrates how to compare two deques in C++:

// C++ Program to illustrate how to compare two deques
#include <deque>
#include <iostream>
using namespace std;

int main()
{
    // Initializing two deques
    deque<int> dq1 = { 10, 20, 30 };
    deque<int> dq2 = { 10, 20, 30 };

    // Comparing the deques
    if (dq1 == dq2) {
        cout << "Both deques are equal" << endl;
    }
    else {
        cout << "Both deques are not equal" << endl;
    }
    return 0;
}

Output
Both deques are equal

Time Complexity: O(N) where N is the number of elements in the deque.
Auxiliary Space: O(1)



Article Tags :