C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Multiset operator==C++ Multiset operator== is a non-member overloaded function of multiset in C++. This function is used to check whether the two multisets are equal or not. Note: Comparison between multiset objects is based on a pair wise comparison of the elements. Two multisets are equal if they have the same number of elements and their corresponding elements have the same values. Otherwise, they are not equal.Syntaxtemplate <class T, class Compare, class Allocator> bool operator== ( const multiset<T,Compare,Allocator>& lhs, const multiset<T,Compare,Allocator>& rhs ); Parameterlhs: First multiset object. rhs: Second multiset object. Return valueIt returns true if the left side of the multiset object is equal to the right side of the multiset 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 multiset objects are always safe means their elements are immutable. Exception SafetyThis function does not throw an exception. Example 1Let's see the simple example to check whether the two multisets are equal or not: #include <iostream> #include <set> using namespace std; int main() { multiset<char> m1; multiset<char> m2; if (m1 == m2) cout << "Both multisets are equal." << endl; m1.emplace('a'); //after adding element in multiset m1 if (!(m1 == m2)) cout << "Both multisets are not equal." << endl; return 0; } Output: Both multisets are equal. Both multisets are not equal. In the above example, multiset m1 and m2 are empty. Therefore operator== will return true and after adding one element in multiset m1, size of m1 becomes different to size of m2. Therefore, it will return false. Example 2Let's see a simple example: #include <set> #include <iostream> int main () { using namespace std; multiset <int> m1, m2, m3; int i; for (i = 0; i <3; i ++) { m1.insert (i); m2.insert (i * i); m3.insert (i); } if (m1 == m2) cout << "The multisets m1 and m2 are equal." << endl; else cout << "The multisets m1 and m2 are not equal." << endl; if (m1 == m3) cout << "The multisets m1 and m3 are equal." << endl; else cout << "The multisets m1 and m3 are not equal." << endl; return 0; } Output: The multisets m1 and m2 are not equal. The multisets m1 and m3 are equal. Example 3Let'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 ; s2 . insert ( 40 ); cout << ( s1 == s2 ) << endl ; } Output: 1 0 In the above example, if multisets s1 and s2 are equal, then it will return 1 otherwise 0. Example 4#include <set> #include <iostream> using namespace std; int main () { multiset<string> m2; typedef multiset<string> login; m2 = {"xyz@123"} ; //stored password string password; login m1; cout<<"---------Login----------"<<endl<<endl; cout<<"Enter the 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 (m1 == m2) cout << "\nWelcome to your Page..." << endl; else cout << "\nIncorrect Password..." << endl; return 0; } Output: 1). ---------Login---------- Enter the password: xyz Password you have entered: xyz Password stored in the system : [email protected] Incorrect Password... 2). ---------Login---------- Enter the password: [email protected] Password you have entered: [email protected] Password stored in the system : [email protected] Welcome to your Page... In the above example, there are two multisets m1 and m2. m1 contains password and second multiset m2 stores user's entered password. It checks whether the both multiset has same elements or not. If password will match then login is successful otherwise, login fails.
Next TopicC++ multiset
|