C++ Multimap 中的 operator>函数

首页 / C++入门教程 / C++ Multimap 中的 operator>函数

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

operator> - 语法

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

operator> - 参数

lhs :第一个multimap对象。

rhs :第二个multimap对象。

operator> - 返回值

如果multimap集合对象的左侧大于multimap对象的右侧,则返回 true ,否则返回 false

operator> - 例子1

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

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-multimap-non-member-operator>-function.html

来源:LearnFk无涯教程网

#include <iostream>  
#include <map>  
  
using namespace std;  
  
int main() {  
   multimap<char, int> m1;  
   multimap<char, int> m2;  
  
   m1.emplace('a', 100);  
  
   if (m1 > m2)  
      cout << "Multimap m1 is greater than m2." << endl;  
  
   m1 = m2;  
  
   if (!(m1 > m2))  
      cout << "Multimap m1 is not greater than m2." << endl;  
  
   return 0;  
}  

输出:

Multimap m1 is greater than m2.
Multimap m1 is not greater than m2.

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

operator> - 例子2

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

#include <map>    
#include <iostream>    
  
using namespace std;  
  
int main( )    
{    
   
   multimap < int, int > m1, m2, m3;    
   int i;    
   typedef pair < int, int > Int_Pair;    
    
   for ( i = 0 ; i < 3 ; i++ )    
   {    
      m1.insert ( Int_Pair ( i, i ) );    
      m2.insert ( Int_Pair ( i, i * i ) );    
      m3.insert ( Int_Pair ( i, i - 1 ) );    
   }    
    
   if ( m1 > m2 )    
      cout << "The multimap m1 is greater than the multimap m2." << endl;    
   else    
      cout << "The multimap m1 is not greater than the multimap m2." << endl;    
    
   if ( m1 > m3 )    
      cout << "The multimap m1 is greater than the multimap m3." << endl;    
   else    
      cout << "The multimap m1 is not greater than the multimap m3." << endl;    
        
      return 0;  
}    

输出:

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

operator> - 例子3

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

#include <iostream>  
 #include <map>  
   
 using namespace std;  
  
int  main ()   
{   
  multimap < char , int >  m1 ,  m2 ;   
  m1.insert ( make_pair ( 'a' , 10 ));   
  m1.insert ( make_pair ( 'b' , 20 ) );   
  m1.insert ( make_pair ( 'c' , 30 ));   
  m2  =  m1 ;  
  
  cout  <<  ( m1  <  m2 )  <<  endl ;  
  
  m2.insert ( make_pair ( 'd' , 40 ));  
  
  cout  <<  ( m1  <  m2 )  << endl ;  
  
  return  0 ;   
}  

输出:

0
1

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

operator> - 例子4

#include <map>    
#include <iostream>   
  
using namespace std;   
    
int main ()    
{    
   multimap<int,string> m2;  
   typedef multimap<int,string> login;   
     
   m2.insert({2040, "xyz@123"}) ; //stored id and password  
     
   string password;  
   int id;  
   login m1;  
     
       cout<<"---------Login----------"<<endl<<endl;  
       cout<<"Enter the ID and password: \n";  
       cin>> id;             //Get key  
       cin>> password;      //Get value  
       m1.insert({id, password});  //Put them in multimap  
  
     cout<<"ID and password you have entered: \n";  
     for (auto it = m1.begin(); it != m1.end(); it++) {  
        cout << (*it).first << "    " << (*it).second << endl;  
      }  
      cout<<"ID and Password stored in the system :\n";  
     for (auto it = m2.begin(); it != m2.end(); it++) {  
        cout << (*it).first << "    " << (*it).second << endl;  
     }  
  
    
   if (m2 > m1)    
        cout << "\nIncorrect ID or Password..." << endl;  
   else    
        cout << "\nWelcome to your Page..." << endl;  
      return 0;  
}  

输出:

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

Enter the ID and password: 
1020     xyz
ID and password you have entered: 
1020     xyz
ID and Password stored in the system:
2040     xyz@123

Incorrect ID or Password...


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

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

Welcome to your Page...

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

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

技术教程推荐

算法面试通关40讲 -〔覃超〕

OAuth 2.0实战课 -〔王新栋〕

技术管理案例课 -〔许健〕

人人都用得上的写作课 -〔涵柏〕

手把手带你搭建秒杀系统 -〔佘志东〕

全链路压测实战30讲 -〔高楼〕

李智慧 · 高并发架构实战课 -〔李智慧〕

商业思维案例笔记 -〔曹雄峰〕

Vue 3 企业级项目实战课 -〔杨文坚〕

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