Set Operator - C++ Programming Tutorial
C++ Course / STL Set & Map / Set Operator

Set Operator

BLUF: Mastering Set Operator is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Set Operator

C++ is renowned for its efficiency. Learn how Set Operator enables low-level control and high-performance computing in the tutorial below.

The operator> is an overloaded function outside the scope of the set class in C++. Its purpose is to determine if the first set is larger than the second set.

Note: Operator> sequentially compares the element of set and comparison will stop at first mismatch.

Syntax

Example

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

Parameter

lhs : First set object.

rhs : Second set object.

Return value

It returns true if the elements on the left side of the set container object are greater than the elements on the right side of the set object; otherwise, it returns false.

Complexity

Complexity remains constant when the sizes of the left-hand side (lhs) and right-hand side (rhs) are unequal.

Alternatively, up to linear in the size of the left-hand side (lhs) and the right-hand side (rhs).

Iterator validity

No changes.

Data Races

Containers, lhs and rhs are accessed.

Simultaneously retrieving the elements of an unaltered set is consistently secure.

Exception Safety

This function does not throw an exception.

Example 1

Let's consider a basic example to verify if the initial set is larger than the second set:

Example

#include <iostream>
#include <set>

using namespace std;

int main() {
   set<char> m1;
   set<char> m2;

   m1.emplace('a');

   if (m1 > m2)
      cout << "Set m1 is greater than m2." << endl;

   m1 = m2;

   if (!(m1 > m2))
      cout << "Set m1 is not greater than m2." << endl;

   return 0;
}

Output:

Output

Set m1 is greater than m2.
Set m1 is not greater than m2.

In the provided scenario, there are two sets named set1 and set2. Set1 comprises a single element, whereas set2 is currently void of any elements. Upon comparing these two sets, a message "set set1 is larger than set2" will be shown. Subsequently, if all elements from set2 are transferred to set1, resulting in both sets containing an equal number of elements, the message displayed will indicate that "set set1 is not larger than set2".

Example 2

Let's see a simple example:

Example

#include <set>  
#include <iostream>  

using namespace std;

int main( )  
{  
 
   set < int> m1, m2, m3;  
   int i;  
  
   for ( i = 0 ; i < 3 ; i++ )  
   {  
      m1.insert ( i );  
      m2.insert (i * i );  
      m3.insert (i - 1 );  
   }  
  
   if ( m1 > m2 )  
      cout << "The set m1 is greater than the set m2." << endl;  
   else  
      cout << "The set m1 is not greater than the set m2." << endl;  
  
   if ( m1 > m3 )  
      cout << "The set m1 is greater than the set m3." << endl;  
   else  
      cout << "The set m1 is not greater than the set m3." << endl;  
      
      return 0;
}

Output:

Output

The set m1 is not greater than the set m2.
The set m1 is greater than the set m3.

Example 3

Let's see a simple example:

Example

#include <iostream>
#include <set>
 
using namespace std;

int  main () 
{ 
  set < int >  s1 ,  s2 ; 
  s1 . insert ( 10 ); 
  s1 . insert ( 20 ); 
  s1 . insert ( 30 ); 
  s2  =  s1 ;

  cout  <<  ( s1  >  s2 )  << endl ;

  s1 . insert ( 40 );

  cout  <<  ( s1  >  s2 )  << endl ; 
}

Output:

If the value of s1 exceeds that of s2 in the aforementioned example, the function will output 1; otherwise, it will return 0.

Example 4

Example

#include <set>  
#include <iostream>  

using namespace std; 
  
int main ()  
{  
   set<string> m2;
   typedef set<string> login; 
   
   m2 = {"xyz@123"} ; //stored id and password
   
   string password;
   login m1;
   
       cout<<"---------Login----------"<<endl<<endl;
       cout<<"Enter password: \n";
       cin>> password;       // Get value
       m1.insert(password);   // Put them in set

     cout<<"Password you have entered: \n";
     for (auto it = m1.begin(); it != m1.end(); it++) {
        cout << (*it)<< endl;
      }
      cout<<"Password stored in the system :\n";
     for (auto it = m2.begin(); it != m2.end(); it++) {
        cout << (*it)<< endl;
     }

   if (m2 > m1)  
        cout << "\nIncorrect Password..." << endl;
   else  
        cout << "\nWelcome to your Page..." << endl;
      return 0;
}

Output:

Output

1).
---------Login----------

Enter password: 
xyz@123
Password you have entered: 
xyz@123
Password stored in the system :
xyz@123

Welcome to your Page...

2).
---------Login---------

Enter password: 
abc@123
Password you have entered: 
abc@123
Password stored in the system:
xyz@123

Incorrect Password...

In the scenario described, there are two collections: m1 and m2. m1 holds the saved password, while m2 stores the password entered by the user. The system verifies if the password in m2 is greater than the one in m1. If the password in m2 is not greater than the one in m1, the login attempt is considered successful; otherwise, the login fails.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience