C++ Standard Library C++ STL Library

C++ <deque> - operator<= Function



The C++ <deque> operator<= function is used to check whether the first deque is less than or equal to the second deque or not. It returns true if the first deque is less than or equal to the second deque, else returns false. operator<= compares elements of deques sequentially and stops comparison after first mismatch.

Syntax

template <class T, class Alloc>
bool operator<= (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);
template <class T, class Alloc>
bool operator<= (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);

Parameters

lhs First deque.
rhs Second deque.

Return Value

Returns true if the contents of lhs are lexicographically less than or equal to 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 deque is less than or equal to the second deque or not.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> dq1 {10, 20, 30};
  deque<int> dq2 {10, 20};
  deque<int> dq3 {40, 50, 60};

  if (dq1 <= dq2)
    cout<<"dq1 is less than or equal to dq2.\n";
  else
    cout<<"dq1 is not less than or equal to dq2.\n";

  if (dq1 <= dq3)
    cout<<"dq1 is less than or equal to dq3.\n";
  else
    cout<<"dq1 is not less than or equal to dq3.\n";
    
  return 0;
}

The output of the above code will be:

dq1 is not less than or equal to dq2.
dq1 is less than or equal to dq3.

❮ C++ <deque> Library