C++ multimap operator<=
C++ operator <= 是 multimap 的一个非成员重载函数。此函数用于检查第一个 multimap 是否小于或等于其他 multimap。
注意:Operator<= 按顺序逐个比较元素,一旦出现不匹配,比较将停止。
语法
template
bool operator<= ( const multimap& lhs,
const multimap& rhs );
参数
lhs :第一个multimap对象。
rhs :第二个multimap对象。
返回值
如果multimap对象的左侧小于或等于右侧,则返回 true ;否则返回 false 。
复杂度
如果lhs和rhs的大小不同,复杂度为常数。
否则,复杂度为线性(等式比较)。
迭代器有效性
没有更改。
数据竞争
访问容器lhs和rhs。
异常安全性
此函数不会抛出异常。
示例1
让我们看一个简单的示例,检查第一个multimap是否小于或等于第二个multimap:
#include
#include
using namespace std;
int main() {
multimap m1;
multimap m2;
m1.emplace('a', 1);
m2.emplace('a', 1);
if (m1 <= m2)
cout << "Multimap m1 is less than or equal to m2." << endl;
m1.emplace('b', 2);
if (!(m1 <= m2))
cout << "Multimap m1 is not less than or equal to m2." << endl;
return 0;
}
输出:
Multimap m1 is less than or equal to m2.
Multimap m1 is not less than or equal to m2.
在上面的示例中,有两个multimap m1和m2。m1和m2都包含一个元素。当我们比较这两个multimap时,会显示消息“multimap m1小于等于m2”,然后在m1中添加一个额外的元素后,会显示消息“multimap m1不小于等于m2”。
示例2
让我们看一个简单的示例:
#include
#include
int main ()
{
using namespace std;
multimap m1, m2, m3, m4;
int i;
typedef pair 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 << "The multimap m1 is less than or equal to the multimap m2." << endl;
else
cout << "The multimap m1 is greater than the multimap m2." << endl;
if (m1 <= m3)
cout << "The multimap m1 is less than or equal to the multimap m3." << endl;
else
cout << "The multimap m1 is greater than the multimap m3." << endl;
if (m1 <= m4)
cout << "The multimap m1 is less than or equal to the multimap m4." << endl;
else
cout << "The multimap m1 is greater than the multimap m4." << endl;
}
输出:
The multimap m1 is less than or equal to the multimap m2.
The multimap m1 is greater than the multimap m3.
The multimap m1 is less than or equal to the multimap m4.
示例3
让我们看一个简单的示例:
#include
#include
using namespace std;
int main()
{
multimap m1, m2;
m1.insert(make_pair('a',10));
m1.insert(make_pair('b',20));
m1.insert(make_pair('c',30));
m2 = m1;
cout << (m1 <= m2) << endl;
m1.insert(make_pair('d',40));
cout << (m1 <= m2) << endl;
return 0;
}
输出:
1
0
在上面的示例中,如果m1小于或等于m2,则返回1,否则返回0。
示例4
#include
#include
using namespace std;
int main ()
{
multimap m2;
typedef multimap login;
m2.insert({2040, "xyz@123"}); //stored id and password
string password;
int id;
login m1;
cout<<"---------Login----------"<> 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 (m2 <= m1)
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...
在上面的示例中,有两个多重映射 m1 和 m2。m1 包含存储的 ID 和密码,第二个多重映射 m2 存储用户输入的 ID 和密码。它检查 m2 是否小于或等于 m1。如果 m2 的 ID 和密码小于或等于 m1,则登录成功,否则登录失败。