Relational Operators on STL Array in C++


There are six relational operators for comparison of operands of the same type. These are >, <, ==, <=, >=, !=. This article contains explanations of these relational operators on STL arrays in C++. The major two operators used in STL arrays are equality comparison (==) and less-than comparison (<) between two array containers.

Equality (==) comparison starts comparing elements of both arrays on either side. Starting from the first elements of both arrays on L.H.S and R.H.S of == operator, the comparison stops at the first mismatch.

Less-than (<) comparison works in lexicographic manner. The algorithm works like std::lexicographic_compare algorithm. The comparison works sequentially using operator(<) in reciprocal manner (check a<b and b<a both). Lexicographic order is used in dictionaries to sort words alphabetically starting from the first letter till the end. The other operators use == and < as shown below −

  • a!=b is equivalent to !(a==b)
  • a>b is equivalent to (b<a)
  • a<=b is equivalent to !(b<a)
  • a>=b is equivalent to !(a<b)

These operators are overloaded in the <header> array.

Note: Both STL arrays on L.H.S. and R.H.S. should have the same parameters <Type,Length>.

The Time Complexity of comparisons is linear time and depends on the size of the arrays. O(n)

The return value is true if conditions hold or false otherwise.

Example

 Live Demo

#include <iostream>
#include <stdio.h>
#include <array>
using namespace std;
int main(){
   // declaration of array
   array<int, 5> marks1 = { 10,20,30,40,50 };
   array<int, 5> marks2 = { 100,200,300,400,500 };
   array<int, 5> marks3 = { 10,20,30,40,50 };
   //a>=b is equivalent to !(a<b) as shown
   if (marks1 >= marks2){
       cout << "Marks1 is greater than equal to Marks2\n";
   }
   else{
      cout << "Marks1 is neither greater nor equal to Marks2\n";
   }
   if (!(marks1 < marks2)){
      cout << "Marks1 is greater than equal to Marks2\n";
   }
   else{
      cout << "Marks1 is neither greater nor equal to Marks2\n";
   }
   //a<=b is equivalent to !(a>b) as shown
   if (marks1 <= marks2){
      cout << "Marks1 is less than equal to Marks2\n";
   }
   else{
      cout << "Marks1 is neither less nor equal to Marks2\n";
   }
   if (!(marks1 > marks2)){
       cout << "Marks1 is less than equal to Marks2\n";
   }
   else{
      cout << "Marks1 is neither less nor equal to Marks2\n";
   }
   //a!=b is equivalent to !(a==b) as shown
   if (marks1 != marks3){
      cout << "Marks1 is not equal to Marks2\n";
   }
   else{
      cout << "Marks1 is equal to Marks2\n";
   }
   if (!(marks1 == marks3)){
      cout << "Marks1 is not equal to Marks2\n";
   }
   else{
      cout << "Marks1 is equal to Marks2\n";
   }
   return 0;
}

Output

Marks1 is neither greater nor equal to Marks2
Marks1 is neither greater nor equal to Marks2
Marks1 is less than equal to Marks2
Marks1 is less than equal to Marks2
Marks1 is equal to Marks2
Marks1 is equal to Marks2

Updated on: 28-Jul-2020

211 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements