C++ multiset operator==
C++ multiset operator==
C++ multiset 运算符 operator == 是C++中 multiset 的非成员重载函数 。此功能用于检查两个 multiset 是否相等。
注意: multiset 对象之间的比较是基于元素的成对比较。如果两个 multiset 具有相同数量的元素并且其对应元素具有相同的值,则它们相等。否则,它们是不相等的。
语法
template <class T, class Compare, class Allocator>
bool operator== ( const multiset<T,Compare,Allocator>& lhs,
const multiset<T,Compare,Allocator>& rhs );
参数
lhs : 第一个 multiset 对象。
rhs : 第二个 multiset 对象。
返回值
如果 multiset 对象的左侧等于 multiset 对象的右侧,则返回true,否则返回false。
复杂度
如果lhs和rhs的大小不同,则复杂度将是恒定的。
否则,lhs和rhs的大小将达到线性。
迭代器有效性
没有变化。
数据竞争
容器,lhs和可以访问rhs。
同时访问未修改的 multiset 对象的元素始终是安全的,这意味着它们的元素是不可变的。
异常安全性
此函数不会引发异常。
示例1
让我们看一个简单的示例来检查两个 multiset 是否相等:
#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;
}
输出:
Both multisets are equal.
Both multisets are not equal.
在上面的示例中, multiset m1和m2为空。因此,operator ==将返回true,并且在 multiset m1中添加一个元素后,m1的大小将不同于m2的大小。因此,它将返回false。
示例2
让我们看一个简单的示例:
#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;
}
输出:
The multisets m1 and m2 are not equal.
The multisets m1 and m3 are equal.
示例3
让我们看一个简单的示例:
#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 ;
}
输出:
在上面的示例中,如果 multiset s1和s2相等,则它将返回1,否则将返回0。
示例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;
}
输出:
1).
---------Login----------
Enter the password:
xyz
Password you have entered:
xyz
Password stored in the system :
xyz@123
Incorrect Password...
2).
---------Login----------
Enter the password:
xyz@123
Password you have entered:
xyz@123
Password stored in the system :
xyz@123
Welcome to your Page...
在上面的示例中,有两个 multiset m1和m2、 m1包含密码,第二个 multiset m2存储用户输入的密码。它检查两个 multiset 是否具有相同的元素。如果密码匹配,则登录成功,否则,登录失败。