C++ std operator>
The C++ Multiset Operator> function is an overloaded non-member function of the multiset class. Its purpose is to determine if the first multiset is greater than the second multiset.
Note: Operator> sequentially compares the element of multiset and comparison will stop at first mismatch.
Syntax
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-hand side of the multiset container object is larger than the right-hand side of the multiset object; otherwise, it returns false.
Complexity
The complexity will remain constant even when the lengths of the left-hand side (lhs) and right-hand side (rhs) are not equal.
Alternatively, the complexity can scale linearly based on the sizes of lhs and rhs.
Iterator validity
No changes.
Data Races
Containers, lhs and rhs are accessed.
Simultaneously retrieving the items from an unchanged multiset is consistently secure.
Exception Safety
This function does not throw an exception.
Example 1
Let's examine a basic example to determine if the initial multiset is greater than another.
#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:
Multiset m1 is greater than m2.
Multiset m1 is not greater than m2.
In the scenario described, there are two multisets m1 and m2. The multiset m1 comprises a single element while m2 is currently devoid of any elements. Upon comparison of these two multisets, the outcome will indicate that "multiset m1 is greater than m2". Subsequently, following the assignment of m2 to m1 resulting in both multisets containing the same elements, the message will convey that "multiset m1 is not greater than m2".
Example 2
Let's see a simple 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:
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:
#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:
If s1 is larger than s2 in the example mentioned, the function will output 1; otherwise, it will return 0.
Example 4
#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:
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 sets m1 and m2. Set m1 holds the saved password, while set m2 contains the password entered by the user. The system verifies if set m2 is larger than set m1. A successful login occurs when the password in set m2 is not greater than that in set m1; otherwise, the login attempt fails.