📜  C++ STL-multimap.operator!=()函数

📅  最后修改于: 2020-10-20 06:12:26             🧑  作者: Mango

C++ STL Multimap.operator!=

C++运算符!=是multimap的非成员重载函数。此函数用于检查两个Multimap是否相等。

注意:Multimap对象之间的比较基于元素的成对比较。如果两个映射具有相同数量的元素并且其对应元素具有相同的值,则它们是相等的。否则,它们是不相等的。

句法

template 
  bool operator!= ( const multimap& lhs,
                    const multimap& rhs );

参数

lhs:第一个Multimap对象。

rhs:第二个Multimap对象。

返回值

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

复杂度

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

否则,大小最大为线性(相等比较)。

迭代器有效性

没有变化。

数据竞争

可以访问容器lhs和rhs。

异常安全

此函数不会引发异常。

例子1

让我们看一个简单的示例,检查两个Multimap是否相等:

#include 
#include 

using namespace std;

int main() {
   multimap m1;
   multimap m2;

   m1.emplace('a', 1);

   if (m1 != m2)
      cout << "Both multimaps are not equal." << endl;

   m1 = m2;

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

   return 0;
}

输出:

Both multimaps are not equal.
Both multimaps are equal.

在上面的示例中,有两个Multimapm1和m2。 m1包含一个元素,m2为空。当我们比较两个Multimap时,它将显示消息“两个Multimap都不相等”,并且在将m1分配给m2之后,两个Multimap具有相等的元素,然后它将显示消息“两个Multimap都相等”。

例子2

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

#include   
#include   
  
int main ()  
{  
   using namespace std;  
   multimap  m1, m2, m3;  
   int i;  
   typedef pair  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));  
   }  
  
   if (m1!= m2)  
      cout << "The multimaps m1 and m2 are not equal." << endl;  
   else  
      cout << "The multimaps m1 and m2 are equal." << endl;  
  
   if (m1!= m3)  
      cout << "The multimaps m1 and m3 are not equal." << endl;  
   else  
      cout << "The multimaps m1 and m3 are equal." << endl;  
      
      return 0;
}

输出:

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

例子3

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

#include 
#include 
using namespace std;

int main()
{
  multimap m1, m2;

  m1.insert(make_pair(1,'a'));
  m1.insert(make_pair(2,'b'));
  m1.insert(make_pair(3,'c'));

  m2 = m1;

  cout << (m1 != m2) << endl;

  m2.insert(make_pair(4,'d'));

  cout << (m1 != m2) << endl;

  return 0;
}

输出:

0
1

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

例子4

#include   
#include   

using namespace std; 
  
int main ()  
{  
   multimap m2;
   typedef multimap login; 
   
  m2.insert({2040, "xyz@123"}) ; //stored id and password
   
   string password;
   int id;
   login m1;
   
       cout<<"---------Login----------"<> 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 (m1 != m2)  
      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和密码。它检查两个Multimap是否具有相同的元素。如果ID和密码匹配,则登录成功,否则,登录失败。