C++ unordered_map::operator!= 函式



C++ 的std::unordered_map::operator!=函式用於檢查兩個unordered_map是否相等。如果兩個unordered_map不相等,則返回true,否則返回false。

如果我們比較資料型別不同的unordered_map,則unordered_map::operator!=函式將顯示錯誤。它僅適用於具有相同資料型別的unordered_map。

語法

以下是std::unordered_map::operator!=函式的語法。

bool operator!=(const unordered_map<Key,T,Hash,Pred,Alloc>& first,
                const unordered_map<Key,T,Hash,Pred,Alloc>& second);

引數

  • first − 第一個unordered_map物件。
  • second − 第二個unordered_map物件。

返回值

如果兩個unordered_map不相等,則此函式返回true,否則返回false。

示例 1

在下面的示例中,讓我們看看operator!=函式的用法。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<char, int> um1;
   unordered_map<char, int> um2;
   um1.emplace('a', 1);
   if (um1 != um2)
      cout << "Both unordered_maps are not equal" << endl;
   um1 = um2;
   if (!(um1 != um2))
      cout << "Both unordered_maps are equal" << endl;
   return 0;
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

Both unordered_maps are not equal
Both unordered_maps are equal

示例 2

考慮下面的示例,我們將比較兩個型別相同但儲存不同元素的unordered_map。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<int, int> um1 = {{1, 5}, {2, 6}, {3, 7}, {4, 8}};
   unordered_map<int, int> um2 = {{2, 4}, {3, 1}, {4, 2}, {5, 3}};
   if (um1 != um2)
      cout << "Both unordered_maps are not equal" << endl;
   if (!(um1 != um2))
      cout << "Both unordered_maps are equal" << endl;
   return 0;
}

輸出

如果我們執行上面的程式碼,它將生成以下輸出:

Both unordered_maps are not equal

示例 3

讓我們看看下面的示例,我們將比較兩個unordered_map並顯示元素。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<string, string> um1 = {{"aman", "kumar"}, {"akash", "gupta"}, {"vivek", "verma"}, {"Rahul", "yadav"}};
   unordered_map<string, string> um2 = {{"Vivek", "Verma"}, {"Alok", "Gupta"}, {"Sakshi", "Sharma"}, {"Akash", "Kumar"}};
   if (um1 != um2) {
      cout<<"elements of um1"<<endl;
      for(auto & it: um1){
         cout<<it.first<<" : "<<it.second<<endl;
      }
      cout<<endl;
      cout<<"elements of um2"<<endl;
      for(auto & it: um2){
         cout<<it.first<<" : "<<it.second<<endl;
      }
   }
   if (!(um1 != um2)){
      cout << " elements of both unordered_maps are equal" << endl;
      for(auto& it: um1){
         cout<<it.first<<" : "<<it.second<<endl;
      }
   }
   return 0;
}

輸出

以下是上述程式碼的輸出:

elements of um1
vivek : verma
Rahul : yadav
akash : gupta
aman : kumar

elements of um2
Akash : Kumar
Sakshi : Sharma
Alok : Gupta
Vivek : Verma

示例 4

以下是一個示例,我們將使用unordered_map::operator!=函式並顯示布林值。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<string, string> um1 = {{"aman", "kumar"}, {"akash", "gupta"}, {"vivek", "verma"}, {"Rahul", "yadav"}};
   unordered_map<string, string> um2 = {{"Vivek", "Verma"}, {"Alok", "Gupta"}, {"Sakshi", "Sharma"}, {"Akash", "Kumar"}};
   if(um1!=um2)
      cout<<(um1!=um2)<<endl;
   else
      cout<<(!(um1!=um2))<<endl;
   return 0;
}

輸出

上述程式碼的輸出如下:

1
廣告