C++ operator == 是Multimap的非成员重载函数。此函数用于检查两个multimap是否相等。
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 );
lhs :第一个multimap对象。
rhs :第二个multimap对象。
如果multimap对象的左侧等于multimap对象的右侧,则返回true,否则返回false。
让我们看一个简单的示例,检查两个multimap是否相等:
#include <iostream> #include <map> using namespace std; int main() { multimap<char, int> m1; multimap<char, int> m2; if (m1 == m2) cout << "Both multimaps are equal." << endl; m1.emplace('a', 10); //after adding element in multimap m1 if (!(m1 == m2)) cout << "Both multimaps are not equal." << endl; return 0; }
输出:
Both multimaps are equal. Both multimaps are not equal.
在上面的示例中,multimapm1和m2为空。因此,operator ==将返回true,并且在multimapm1中添加一个元素后,m1的大小变得与m2的大小不同。因此,它将返回false。
让我们看一个简单的例子:
#include <map> #include <iostream> int main () { using namespace std; multimap <int, int> m1, m2, m3; int i; typedef pair <int, int> Int_Pair; for (i = 0; i <3; i ++) { m1.insert (Int_Pair (i, i)); m2.insert (Int_Pair (i, i * i)); m3.insert (Int_Pair (i, i)); } if (m1 == m2) cout << "The multimaps m1 and m2 are equal." << endl; else cout << "The multimaps m1 and m2 are not equal." << endl; if (m1 == m3) cout << "The multimaps m1 and m3 are equal." << endl; else cout << "The multimaps m1 and m3 are not equal." << endl; return 0; }
输出:
The multimaps m1 and m2 are not equal. The multimaps m1 and m3 are equal.
让我们看一个简单的例子:
#include <iostream> #include <map> using namespace std; int main () { multimap < int , char > m1 ; m1 . insert ( make_pair ( 1 , 'a' )); auto m2 = m1 ; cout << ( m1 == m2 ) << endl ; m2 . insert ( make_pair ( 1 , 'b' )); m2 . insert ( make_pair ( 2 , 'c' )); cout << ( m1 == m2 ) << endl ; return 0 ; }
输出:
1 0
在上面的示例中,如果m1和m2相等,则它将返回1,否则返回0。
#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; }
输出:
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...
在上面的示例中,有两个multimapm1和m2。 m1包含存储的ID和密码,第二个multimapm2存储用户输入的ID和密码。它检查两个multimap是否具有相同的元素。如果ID和密码匹配,则登录成功,否则登录失败。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)