A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_queue_operator_equal_to.htm below:

C++ Queue Equal To Operator

C++ Queue::operator==() Function

The C++ std::queue::operator==() function is used to check if both the queues are equal by comparing their elements and order. It returns true if both queues contain same elements otherwise it returns false. It is useful for comparing queues efficiently, providing a convenient way to check for equality without iterating through each element. The time complexity of this function is linear i.e.O(n).

Syntax

Following is the syntax for std::queue::operator==() function.

bool operator== (const queue<T,Container>& q1, const queue<T,Container>& q2);
Parameters Return value

This function returns true if both queues are identical otherwise false.

Example

Let's look at the following example, where we are going to initialize the both queue with same elements and applying the operator==() function.

#include <iostream>
#include <queue>
int main()
{
    std::queue<int> a, b;
    a.push(11);
    a.push(222);
    b.push(11);
    b.push(222);
    if (a == b)
        std::cout << "Queues are equal." << std::endl;
    else
        std::cout << "Queues are not equal." << std::endl;
    return 0;
}
Output

Output of the above code is as follows −

Queues are equal.
Example

Consider the another scenario, where we are going to initialize the queues of different size and applying the operator==() function.

#include <iostream>
#include <queue>
int main()
{
    std::queue<int> a, b;
    a.push(11);
    a.push(222);
    b.push(1);
    if (a == b) {
        std::cout << "Queues are equal.";
    } else {
        std::cout << "Queues are not equal.";
    }
    return 0;
}
Output

Let us compile and run the above program, this will produce the following result −

Queues are not equal.
Example

In the following example, we are going to apply the operator==() function on the empty queue and observing the output.

#include <iostream>
#include <queue>
int main()
{
    std::queue<int> a, b;
    if (a == b) {
        std::cout << "Queues are equal.";
    } else {
        std::cout << "Queues are not equal.";
    }
    return 0;
}
Output

If we run the above code it will generate the following output −

Queues are equal.
Example

Following is the example, where we are going to initialize the queue with same number of elements with different values and applying operator==() function.

#include <iostream>
#include <queue>
int main()
{
    std::queue<int> x, b;
    x.push(11);
    x.push(2);
    b.push(222);
    b.push(33);
    if (x == b) {
        std::cout << "Queues are equal.";
    } else {
        std::cout << "Queues are not equal.";
    }
    return 0;
}
Output

Following is the output of the above code −

Queues are not equal.

queue.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4