The C++ operator>= represents a non-member overloaded method of multimap in C++. Its purpose is to verify if the initial multimap is greater than or equal to another multimap.
Note: Operator >= sequentially compares the element of multimap and comparison will stop at first mismatch.
Syntax
template <class Key, class T, class Compare, class Alloc>
bool operator>= ( const multimap<Key,T,Compare,Alloc>& lhs,
const multimap<Key,T,Compare,Alloc>& rhs );
Parameter
lhs : First multimap object.
rhs : Second multimap object.
Return value
It returns true if the key on the left side of the multimap container object is greater than or equal to the key on the right side of the multimap container object; otherwise, it returns false.
Complexity
The complexity remains constant when the sizes of the left-hand side (lhs) and right-hand side (rhs) are not equal.
Otherwise, scaling linearly with the size (equality comparisons).
Iterator validity
No changes.
Data Races
Containers, lhs and rhs are accessed.
Exception Safety
This function does not throw an exception.
Example 1
Let's examine a basic example to verify if the initial multimap is greater than or equal to another.
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<char, int> m1;
multimap<char, int> m2;
m1.emplace('a', 1);
m2.emplace('a', 1);
if (m1 >= m2)
cout << "Multimap m1 is greater than or equal to m2." << endl;
m2.emplace('b', 2);
if (!(m1 >= m2))
cout << "Multimap m1 is not greater than or equal to m2." << endl;
return 0;
}
Output:
Multimap m1 is greater than or equal to m2.
Multimap m1 is not greater than or equal to m2.
In the scenario provided, there are two multimaps denoted as m1 and m2, each holding a single element. Upon comparing the two multimaps, the output will indicate that "multimap m1 is greater than or equal to m2." However, upon adding an additional element to m2, the result will change to indicate that "multimap m1 is not greater than or equal to m2."
Example 2
Let's see a simple example:
#include <map>
#include <iostream>
using namespace std;
int main( )
{
multimap < int, int > m1, m2, m3, m4;
int i;
typedef pair < int, int > Int_Pair;
for ( i = 1 ; i < 3 ; i++ )
{
m1.insert ( Int_Pair ( i, i ) );
m2.insert ( Int_Pair ( i, i * i ) );
m3.insert ( Int_Pair ( i, i - 1 ) );
m4.insert ( Int_Pair ( i, i ) );
}
if ( m1 >= m2 )
cout << "Multimap m1 is greater than or equal to multimap m2." << endl;
else
cout << "The multimap m1 is less than the multimap m2." << endl;
if ( m1 >= m3 )
cout << "Multimap m1 is greater than or equal to multimap m3." << endl;
else
cout << "The multimap m1 is less than the multimap m3." << endl;
if ( m1 >= m4 )
cout << "Multimap m1 is greater than or equal to multimap m4." << endl;
else
cout << "The multimap m1 is less than the multimap m4." << endl;
return 0;
}
Output:
The multimap m1 is less than the multimap m2.
Multimap m1 is greater than or equal to multimap m3.
Multimap m1 is greater than or equal to multimap m4.
Example 3
Let's see a simple example:
#include <iostream>
#include <map>
using namespace std;
int main()
{
multimap<char,int> m1, m2;
m1.insert(make_pair('a',10));
m1.insert(make_pair('b',20));
m1.insert(make_pair('a',30));
m2 = m1;
cout << (m1 >= m2) << endl;
m2.insert(make_pair('d',40));
cout << (m1 >= m2) << endl;
return 0;
}
Output:
If the value of m1 is equal to or greater than m2, the function will output 1; otherwise, it will output 0.
Example 4
#include <map>
#include <iostream>
using namespace std;
int main ()
{
multimap<int,string> m2;
typedef multimap<int,string> login;
m2.insert({2040, "xyz@123"}) ; //stored id and password
string password;
int id;
login m1;
cout<<"---------Login----------"<<endl<<endl;
cout<<"Enter the ID and password: \n";
cin>> id; // Get key
cin>> password; // Get value
m1.insert({id, password}); // Put them in multimap
cout<<"ID and password you have entered: \n";
for (auto it = m1.begin(); it != m1.end(); it++) {
cout << (*it).first << " " << (*it).second << endl;
}
cout<<"ID and Password stored in the system :\n";
for (auto it = m2.begin(); it != m2.end(); it++) {
cout << (*it).first << " " << (*it).second << endl;
}
if (m1 >= m2)
cout << "\nWelcome to your Page..." << endl;
else
cout << "\nIncorrect ID or Password..." << endl;
return 0;
}
Output:
1).
---------Login----------
Enter the ID and password:
1020 xyz
ID and password you have entered:
1020 xyz
ID and Password stored in the system:
2040 xyz@123
Incorrect ID or Password...
2).
---------Login----------
Enter the ID and password:
2040 xyz@123
ID and password you have entered:
2040 xyz@123
ID and Password stored in the system:
2040 xyz@123
Welcome to your Page...
In the provided scenario, there are two multimaps named m1 and m2. m1 holds the stored ID and password, while the second multimap m2 stores the user's inputted ID and password. The comparison is made to determine if m1 is greater than or equal to m2. If the ID and password stored in m1 are greater than or equal to those in m2, the login process is considered successful; otherwise, the login attempt fails.