Multiset Operator

C++ std operator>

C++ Multiset Operator> is a non-member overloaded function of multiset. This function is used to check whether the first multiset is greater than other or not.

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

Syntax

Example

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

Parameter

lhs : First multiset object.

rhs : Second multiset object.

Return value

It returns true if the left side of the multiset container object is greater than the right side of the multiset object otherwise false .

Complexity

Complexity will be constant, if the size of lhs and rhs is different.

Otherwise, up to linear in the size of lhs and rhs.

Iterator validity

No changes.

Data Races

Containers, lhs and rhs are accessed.

Concurrently accessing the elements of unmodified multiset is always safe.

Exception Safety

This function does not throw an exception.

Example 1

Let's see the simple example to check whether the first multiset is greater than or not:

Example

#include <iostream>
#include <set>

using namespace std;

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

   m1.emplace('a');

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

   m1 = m2;

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

   return 0;
}

Output:

Output

Multiset m1 is greater than m2.
Multiset m1 is not greater than m2.

In the above example, there are two multisets m1 and m2. The m1 multiset contains one element and m2 is empty. When we compare both multisets then it will display the message "multiset m1 is greater than m2" and after assigning m2 to m1 both multisets have equal element then it will display the message that "multiset m1 is not greater than m2".

Example 2

Let's see a simple example:

Example

#include <set>  
#include <iostream>  

using namespace std;

int main( )  
{  
 
   multiset < 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 multiset m1 is greater than the multiset m2." << endl;  
   else  
      cout << "The multiset m1 is not greater than the multiset m2." << endl;  
  
   if ( m1 > m3 )  
      cout << "The multiset m1 is greater than the multiset m3." << endl;  
   else  
      cout << "The multiset m1 is not greater than the multiset m3." << endl;  
      
      return 0;
}

Output:

Output

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

Example 3

Let's see a simple example:

Example

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

int  main () 
{ 
  multiset < 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:

In the above example, if s1 is greater than s2, then it will return 1 otherwise 0.

Example 4

Example

#include <set>  
#include <iostream>  

using namespace std; 
  
int main ()  
{  
   multiset<string> m2;
   typedef multiset<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 multiset

     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 above example, there are two multisets m1 and m2. The m1 multiset contains stored password and second multiset m2 stores user's entered password. It checks whether the m2 is greater than m1 or not. If password of m2 is not greater than m1 then login is successful otherwise login fails.

Input Required

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