C++ Standard Library C++ STL Library

C++ queue - operator< Function



The C++ queue operator< function is used to check whether the first queue is less than the second queue or not. It returns true if the first queue is less than the second queue, else returns false.

Syntax

template <class T, class Container>
bool operator< (const queue<T,Container>& lhs, const queue<T,Container>& rhs);
template <class T, class Container>
bool operator< (const queue<T,Container>& lhs, const queue<T,Container>& rhs);

Parameters

lhs First queue.
rhs Second queue.

Return Value

Returns true if the contents of lhs are lexicographically less than the contents of rhs, else returns false.

Time Complexity

Linear i.e, Θ(n).

Example:

In the example below, the operator< function is used to check whether the first queue is less than the second queue or not.

#include <iostream>
#include <queue>
using namespace std;
 
int main (){
  queue<int> q1;
  queue<int> q2;
  
  for(int i = 10; i>5; i--) {
    q1.push(i);
    q2.push(i);
  }

  if (q1 < q2)
    cout<<"q1 is less than q2.\n";
  else
    cout<<"q1 is not less than q2.\n";

  q1.pop();
  cout<<"\nAfter deleting first element of q1.\n";
  if (q1 < q2)
    cout<<"q1 is less than q2.\n";
  else
    cout<<"q1 is not less than q2.\n";
    
  return 0;
}

The output of the above code will be:

q1 is not less than q2.

After deleting first element of q1.
q1 is less than q2.

❮ C++ <queue> Library