📜  C++ STL-Multiset.operator>

📅  最后修改于: 2020-10-20 01:27:38             🧑  作者: Mango

C++ std运算符>

C++ Multiset Operator>是multiset的非成员重载函数。此函数用于检查第一个Multiset是否大于其他Multiset。

注意:Operator>按顺序比较multiset的元素,比较将在第一次不匹配时停止。

句法

template 
  bool operator>  ( const multiset& lhs,
                    const multiset& rhs );

参数

lhs:第一个Multiset对象。

rhs:第二个Multiset对象。

返回值

如果多重集容器对象的左侧大于多重集对象的右侧,则返回true,否则返回false。

复杂度

如果lhs和rhs的大小不同,则复杂度将保持不变。

否则,最大长度为lhs和rhs。

迭代器有效性

没有变化。

数据竞争

可以访问容器lhs和rhs。

同时访问未修改Multiset的元素始终是安全的。

异常安全

此函数不会引发异常。

例子1

让我们看一个简单的示例,检查第一个多重集是否大于:

#include 
#include 

using namespace std;

int main() {
   multiset m1;
   multiset m2;

   m1.emplace('a');

   if (m1 > m2)
      cout << "Multiset m1 is greater than m2." << endl;

   m1 = m2;

   if (!(m1 > m2))
      cout << "Multiset m1 is not greater than m2." << endl;

   return 0;
}

输出:

Multiset m1 is greater than m2.
Multiset m1 is not greater than m2.

在上面的示例中,有两个多重集m1和m2。 m1多重集包含一个元素,m2为空。当我们比较两个多重集时,它将显示消息“多重集m1大于m2″,并且在将m2分配给m1之后,两个多重集具有相等的元素,则它将显示消息“多重集m1不大于m2″。

例子2

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

#include   
#include   

using namespace std;

int main( )  
{  
 
   multiset < int> m1, m2, m3;  
   int i;  
  
   for ( i = 0 ; i < 3 ; i++ )  
   {  
      m1.insert ( i );  
      m2.insert (i * i );  
      m3.insert (i - 1 );  
   }  
  
   if ( m1 > m2 )  
      cout << "The multiset m1 is greater than the multiset m2." << endl;  
   else  
      cout << "The multiset m1 is not greater than the multiset m2." << endl;  
  
   if ( m1 > m3 )  
      cout << "The multiset m1 is greater than the multiset m3." << endl;  
   else  
      cout << "The multiset m1 is not greater than the multiset m3." << endl;  
      
      return 0;
} 

输出:

The multiset m1 is not greater than the multiset m2.
The multiset m1 is greater than the multiset m3.

例子3

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

#include 
#include 
 
using namespace std;

int  main () 
{ 
  multiset < 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 ; 
}

输出:

0
1

在上面的示例中,如果s1大于s2,则它将返回1,否则返回0。

例子4

#include   
#include   

using namespace std; 
  
int main ()  
{  
   multiset m2;
   typedef multiset login; 
   
   m2 = {"xyz@123"} ; //stored id and password
   
   string password;
   login m1;
   
       cout<<"---------Login----------"<> password;       // Get value
       m1.insert(password);   // Put them in multiset

     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 << "\nIncorrect Password..." << endl;
   else  
        cout << "\nWelcome to your Page..." << 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@123
Password you have entered: 
abc@123
Password stored in the system:
xyz@123

Incorrect Password...

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