C++ std operator!= 是set的非成员重载函数。此函数用于检查两组是否相等。
template <class T, class Compare, class Alloc> bool operator!= ( const set<T,Compare,Alloc>& lhs, const set<T,Compare,Alloc>& rhs );
lhs :第一个设置的对象。
rhs :第二组对象。
如果设置对象的左侧等于设置对象的右侧,则返回true,否则返回false。
让我们看一个简单的示例来检查两个集合是否相等:
#include <iostream> #include <set> using namespace std; int main(void) { set<char> m1; set<char> m2; m1.emplace('a'); if (m1 != m2) cout << "Both sets are not equal." << endl; m1 = m2; if (!(m1 != m2)) cout << "Both sets are equal." << endl; return 0; }
输出:
Both sets are not equal. Both sets are equal.
在上面的示例中,有两组m1和m2。 m1包含一个元素,m2为空。当我们比较两个集合时,它将显示消息"两个集合都不相等",并且在将m1分配给m2之后,两个集合具有相等的元素,然后它将显示消息"两个集合都相等"。
让我们看一个简单的例子:
#include <set> #include <iostream> int main () { using namespace std; set <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 sets m1 and m2 are not equal." << endl; else cout << "The sets m1 and m2 are equal." << endl; if (m1!= m3) cout << "The sets m1 and m3 are not equal." << endl; else cout << "The sets m1 and m3 are equal." << endl; return 0; }
输出:
The sets m1 and m2 are not equal. The sets m1 and m3 are equal.
让我们看一个简单的例子:
#include <iostream> #include <set> using namespace std; int main () { set < 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 ; }
输出:
0 1
在上面的示例中,如果集合s1和s2相等,则它将返回1,否则返回0。
#include <set> #include <iostream> using namespace std; int main () { set<string> m2; typedef set<string> login; m2 = {"xyz@123"} ; //stored password string password; login m1; cout<<"---------Login----------"<<endl<<endl; cout<<"Enter password: \n"; cin>> 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 (m1 != m2) cout << "\nIncorrect Password..." << endl; else cout << "\nWelcome to your Page..." << endl; return 0; }
输出:
1). ---------Login---------- Enter password: xyz Password you have entered: xyz Password stored in the system: xyz@123 Incorrect Password... 2). ---------Login---------- Enter password: xyz@123 Password you have entered: xyz@123 Password stored in the system: xyz@123 Welcome to your Page...
在上面的示例中,有两组m1和m2。 m1包含存储的密码,第二组m2存储用户输入的密码。它检查这两个集合是否具有相同的元素。如果密码匹配,则登录成功,否则登录失败。
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-set-non-member-operator!=.html
来源:LearnFk无涯教程网
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)