C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ std operator>=C++ std Operator>= is a non-member overloaded function of set in C++. This function is used to check whether the first set is greater than or equal to other or not. Note: Operator >= sequentially compares the element of set and comparison will stop at first mismatch.Syntaxtemplate <class T, class Compare, class Alloc> bool operator>= ( const set<T,Compare,Alloc>& lhs, const set<T,Compare,Alloc>& rhs ); Parameterlhs: First set object. rhs: Second set object. Return valueIt returns true if the left side of the set container object is greater than or equal to the right side of the set container object otherwise, false. ComplexityComplexity will be constant, if the size of lhs and rhs is different. Otherwise, up to linear in the size of lhs and rhs. Iterator validityNo changes. Data RacesContainers, lhs and rhs are accessed. Concurrently accessing the elements of unmodified set is always safe. Exception SafetyThis function does not throw an exception. Example 1Let's see the simple example to check whether the first set is greater than or equal to or not: #include <iostream> #include <set> using namespace std; int main() { set<char> m1; set<char> m2; m1.emplace('a'); m2.emplace('a'); if (m1 >= m2) cout << "Set m1 is greater than or equal to m2." << endl; m2.emplace('b'); if (!(m1 >= m2)) cout << "Set m1 is not greater than or equal to m2." << endl; return 0; } Output: Set m1 is greater than or equal to m2. Set m1 is not greater than or equal to m2. In the above example, there are two sets m1 and m2. m1 and m2 contain one element. When we compare both sets then it will display the message "set m1 is greater than or equal to m2" and after adding one more element to m2, it will display the message "set m1 is not greater than or equal to m2". Example 2Let's see a simple example: #include <set> #include <iostream> using namespace std; int main( ) { set < int > m1, m2, m3, m4; int i; for ( i = 1 ; i < 3 ; i++ ) { m1.insert ( i ); m2.insert (i * i ); m3.insert ( i - 1 ); m4.insert ( i ); } if ( m1 >= m2 ) cout << "Set m1 is greater than or equal to set m2." << endl; else cout << "The set m1 is less than the set m2." << endl; if ( m1 >= m3 ) cout << "Set m1 is greater than or equal to set m3." << endl; else cout << "The set m1 is less than the set m3." << endl; if ( m1 >= m4 ) cout << "Set m1 is greater than or equal to set m4." << endl; else cout << "The set m1 is less than the set m4." << endl; return 0; } Output: The set m1 is less than the set m2. Set m1 is greater than or equal to set m3. Set m1 is greater than or equal to set m4. Example 3Let's see a simple 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 ; s2 . insert ( 40 ); cout << ( s1 >= s2 ) << endl ; } Output: 1 0 In the above example if m1 is greater than or equal to m2 then it will return 1 otherwise 0. Example 4#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 (m1 >= m2) cout << "\nWelcome to your Page..." << endl; else cout << "\nIncorrect Password..." << endl; return 0; } Output: 1). ---------Login---------- Enter password: [email protected] Password you have entered: [email protected] Password stored in the system : [email protected] Welcome to your Page... 2). ---------Login---------- Enter password: [email protected] Password you have entered: [email protected] Password stored in the system : [email protected] Incorrect Password... In the above example, there are two sets m1 and m2. m1 contains stored password and second set m2 stores user's entered password. It checks whether the m1 is greater than or equal to m2 or not. If password of m1 is greater than or equal to m2 then login is successful otherwise, login fails.
Next TopicSet swap() Function
|