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对象。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

返回值

如果两个unordered_map不相等,则此函数返回true,否则返回false。

示例 1

在下面的示例中,让我们看看operator!=函数的用法。

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

Open Compiler
#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并显示元素。

Open Compiler
#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!=函数并显示布尔值。

Open Compiler
#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
广告