C++ Set 中的 operator==函数

首页 / C++入门教程 / C++ Set 中的 operator==函数

C++ std operator == 是C++中设置的非成员重载函数。此函数用于检查两组是否相等。

Note: 设置对象之间的比较基于元素的成对比较。如果两组元素数量相同且其对应元素值相同,则两组相等。否则,它们是不相等的。

operator== - 语法

template <class T, class Compare, class Alloc>
  bool operator== ( const set<T,Compare,Alloc>& lhs,
                    const set<T,Compare,Alloc>& rhs );

operator== - 参数

lhs :第一个设置的对象。

rhs :第二组对象。

operator== - 返回值

如果设置对象的左侧等于设置对象的右侧,则返回true,否则返回false。

operator== - 例子1

让我们看一个简单的示例来检查两个集合是否相等:

#include <iostream>
#include <set>

using namespace std;

int main() {
   set<char> m1;
   set<char> m2;

   if (m1 == m2)
      cout << "Both sets are equal." << endl;

   m1.emplace('a');
   
   //after adding element in set m1
   if (!(m1 == m2))
      cout << "Both sets are not equal." << endl;

   return 0;
}

输出:

Both sets are equal.
Both sets are not equal.

在上面的示例中,集合m1和m2为空。因此,operator ==将返回true,并且在集合m1中添加一个元素后,m1的大小将变得不同于m2的大小。因此,它将返回false。

operator== - 例子2

让我们看一个简单的例子:

无涯教程网

#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 equal." << endl;  
   else  
      cout << "The sets m1 and m2 are not equal." << endl;  
  
   if (m1 == m3)  
      cout << "The sets m1 and m3 are equal." << endl;  
   else  
      cout << "The sets m1 and m3 are not equal." << endl;  
      return 0;
}  

输出:

The sets m1 and m2 are not equal.
The sets m1 and m3 are equal.

operator== - 例子3

让我们看一个简单的例子:

无涯教程网

#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 ; 
}

输出:

1
0

在上面的示例中,如果集合s1和s2相等,则它将返回1,否则返回0。

operator== - 例子4

#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 the 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 << "\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...

在上面的示例中,有两组m1和m2。 m1包含密码,第二组m2存储用户输入的密码。它检查这两个集合是否具有相同的元素。如果密码匹配,则登录成功,否则登录失败。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

软件测试52讲 -〔茹炳晟〕

从0开始学微服务 -〔胡忠想〕

重学前端 -〔程劭非(winter)〕

雷蓓蓓的项目管理实战课 -〔雷蓓蓓〕

MongoDB高手课 -〔唐建法(TJ)〕

SRE实战手册 -〔赵成〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

大厂广告产品心法 -〔郭谊〕

云计算的必修小课 -〔吕蕴偲〕

好记忆不如烂笔头。留下您的足迹吧 :)