C++ Queue::operator>() Function



The C++ std::queue::operator> function is a relational operator used to compare the objects of the queue. It checks if the first queue is lexicographically greater than the second queue and returns a boolean value true if the element from the first queue is greater than the second queue, otherwise false. 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

  • q1 − It indicates the first queue object.
  • q2 − It indicates the second queue object.

Return value

This function returns true if first queue is greater than second otherwise false.

Example

Let's look at the following example, where we are going to initialize a queues with integers and compare them.

#include <iostream>
#include <queue>
int main()
{
    std::queue<int> a;
    std::queue<int> b;
    a.push(1);
    a.push(22);
    b.push(1);
    b.push(2);
    if (a > b) {
        std::cout << "Queue1 is greater than Queue2." << std::endl;
    } else {
        std::cout << "Queue1 is not greater than Queue2." << std::endl;
    }
    return 0;
}

Output

Output of the above code is as follows −

Queue1 is greater than Queue2.

Example

Consider the another scenario, where we are going to initialize a queue with characters and apply the operator> function.

#include <iostream>
#include <queue>
int main()
{
    std::queue<char> a;
    std::queue<char> b;
    a.push('a');
    a.push('b');
    b.push('a');
    b.push('c');
    if (a > b) {
        std::cout << "Queue1 is greater than Queue2." << std::endl;
    } else {
        std::cout << "Queue1 is not greater than Queue2." << std::endl;
    }
    return 0;
}

Output

Following is the output of the above code −

Queue1 is not greater than Queue2.

Example

In the following example, we are going to apply the operator> function on the queue that contains float values.

#include <iostream>
#include <queue>
int main()
{
    std::queue<float> a;
    std::queue<float> b;
    a.push(1.1);
    a.push(0.4);
    b.push(0.2);
    b.push(0.5);
    if (a > b) {
        std::cout << "Queue1 is greater than Queue2." << std::endl;
    } else {
        std::cout << "Queue1 is not greater than Queue2." << std::endl;
    }
    return 0;
}

Output

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

Queue1 is greater than Queue2.

Example

Following is the example, where 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;
    std::queue<int> b;
    if (a > b) {
        std::cout << "Queue1 is greater than Queue2." << std::endl;
    } else {
        std::cout << "Queue1 is not greater than Queue2." << std::endl;
    }
    return 0;
}

Output

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

Queue1 is not greater than Queue2.
queue.htm
Advertisements