📅  最后修改于: 2020-10-20 08:16:31             🧑  作者: Mango
C++ std Operator <=是C++中set的非成员重载函数。此函数用于检查第一组是否小于或等于其他。
注意:运算符<=按顺序比较元素,并且首先不匹配比较将停止。
template
bool operator<= ( const set& lhs,
const set& rhs );
lhs:第一个Set.的对象。
rhs:第二组对象。
如果Set.对象的左侧小于或等于Set.对象的右侧,则返回true,否则返回false。
如果lhs和rhs的大小不同,则复杂度将保持不变。
否则,最大长度为lhs和rhs。
没有变化。
可以访问容器lhs和rhs。
同时访问未修改集合的元素始终是安全的。
此函数不会引发异常。
让我们看一个简单的示例,检查第一个集合是否小于或等于:
#include
#include
using namespace std;
int main() {
set m1;
set m2;
m1.emplace('a');
m2.emplace('a');
if (m1 <= m2)
cout << "Set m1 is less than or equal to m2." << endl;
m1.emplace('b');
if (!(m1 <= m2))
cout << "Set m1 is not less than or equal to m2." << endl;
return 0;
}
输出:
Set m1 is less than or equal to m2.
Set m1 is not less than or equal to m2.
在上面的示例中,有两组m1和m2。 m1和m2包含一个元素。当我们比较两个集合时,它将显示消息“集合m1小于或等于m2″,并且在m1中添加一个额外的元素后,它将显示消息“集合m1不小于或等于m2″。
让我们看一个简单的例子:
#include
#include
int main ()
{
using namespace std;
set 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 << "The set m1 is less than or equal to the set m2." << endl;
else
cout << "The set m1 is greater than the set m2." << endl;
if (m1 <= m3)
cout << "The set m1 is less than or equal to the set m3." << endl;
else
cout << "The set m1 is greater than the set m3." << endl;
if (m1 <= m4)
cout << "The set m1 is less than or equal to the set m4." << endl;
else
cout << "The set m1 is greater than the set m4." << endl;
}
输出:
The set m1 is less than or equal to the set m2.
The set m1 is greater than the set m3.
The set m1 is less than or equal to the set m4.
让我们看一个简单的例子:
#include
#include
using namespace std;
int main ()
{
set < int > s1 , s2 ;
s1 . insert ( 10 );
s1 . insert ( 20 );
s1 . insert ( 30 );
s2 = s1 ;
cout << ( s1 <= s2 ) << endl ;
s1 . insert ( 40 );
cout << ( s1 <= s2 ) << endl ;
}
输出:
1
0
在上面的示例中,如果集合s1小于或等于s2,则它将返回1,否则返回0。
#include
#include
using namespace std;
int main ()
{
set m2;
typedef set login;
m2.insert("xyz@123") ; //stored password
string password;
login m1;
cout<<"---------Login----------"<> 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 (m2 <= m1)
cout << "\nWelcome to your Page..." << endl;
else
cout << "\nIncorrect Password..." << endl;
return 0;
}
输出:
1).
---------Login----------
Enter password:
xyz@123
Password you have entered:
xyz@123
Password stored in the system :
xyz@123
Welcome to your Page...
2).
---------Login----------
Enter password:
abc@333
Password you have entered:
abc@333
Password stored in the system:
xyz@123
Incorrect Password...
在上面的示例中,有两组m1和m2。 m1包含存储的密码,第二组m2存储用户输入的密码。它检查m2是否小于或等于m1。如果密码m2小于或等于m1,则登录成功,否则登录失败。