C++ Set 中的 operator<函数

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

此函数用于检查第一组是否小于其他组。

Note: Operator < 按顺序比较set的元素,比较将在第一次不匹配时停止。

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;

   m2.emplace('a');

   if (m1 < m2)
      cout << "Set m1 is less than m2." << endl;

   m1 = m2;

   if (!(m1 < m2))
      cout << "Set m1 is not less than m2." << endl;

   return 0;
}

输出:

Set m1 is less than m2.
Set m1 is not less than m2.

在上面的示例中,有两组m1和m2。 m2包含一个元素,而m1为空。当我们比较两个集合时,它将显示消息"集合m1小于m2",并且在将m2分配给m1之后,两个集合具有相等的元素,然后它将显示消息"集合m1不小于m2"。

operator<-  例子2

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

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-set-non-member-operator-less-than.html

来源:LearnFk无涯教程网

#include <set>  
#include <iostream>  
  
int main ()  
{  
   using namespace std;  
   set <int> m1, m2, m3;  
   int i;  
  
   for (i = 1; i <3; i ++)  
   {  
      m1.insert (i);  
      m2.insert (i * i);  
      m3.insert (i - 1);  
   }  
  
   if (m1 <m2)  
      cout << "The set m1 is less than the set m2." << endl;  
   else  
      cout << "The set m1 is not less than the set m2." << endl;  
  
   if (m1 <m3)  
      cout << "The set m1 is less than the set m3." << endl;  
   else  
      cout << "The set m1 is not less than the set m3." << endl;  
}  

输出:

The set m1 is less than the set m2.
The set m1 is not less than the set m3. 

operator<-  例子3

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

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-set-non-member-operator-less-than.html

来源:LearnFk无涯教程网

#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。

operator<-  例子4

#include <set>  
#include <iostream>  
using namespace std; 
  
int main ()  
{  
   set<string> m2;
   typedef set<string> login; 
   
   m2.insert("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 << "\nIncorrect Password..." << endl; 
 
   else  
        cout << "\nWelcome to your Page..." << endl;
      
      return 0;
}

输出:

1).
---------Login----------

Enter the password: 
xyz@123
Password you have entered: 
xyz@123
Password stored in the system :
xyz@123

Welcome to your Page...


2).
---------Login----------

Enter the password: 
abc@123
Password you have entered: 
abc@123
Password stored in the system:
xyz@123

Incorrect Password...

在上面的示例中,有两组m1和m2。 m1包含密码,第二组m2存储用户输入的密码。它检查m1是否小于m2。如果密码m1不小于m2,则登录成功,否则登录失败。

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

技术教程推荐

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

快速上手Kotlin开发 -〔张涛〕

从0开始学大数据 -〔李智慧〕

Java业务开发常见错误100例 -〔朱晔〕

爱上跑步 -〔钱亮〕

技术面试官识人手册 -〔熊燚(四火)〕

说透区块链 -〔自游〕

深入浅出分布式技术原理 -〔陈现麟〕

超级访谈:对话毕玄 -〔毕玄〕

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