C++ unordered_multimap::operator!= 函数



C++ 的std::unordered_multimap::operator!= 函数用于检查两个 unordered_multimap 是否相等。如果两个 unordered_multimap 不相等则返回 true,否则返回 false。

当两个 multimap 具有相同的数据类型时,此函数将起作用。如果尝试比较数据类型不同的 unordered_multimap,则会显示错误。

语法

以下是 std::unordered_multimap::operator!= 函数的语法。

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

参数

  • first - 第一个 unordered_multimap 对象。
  • second - 第二个 unordered_multimap 对象。

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

返回值

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

示例 1

在以下示例中,我们将演示 unordered_multimap::operator!= 函数的使用。

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

输出

让我们编译并运行上述程序,这将产生以下结果:

Both unordered_multimaps are not equal
Both unordered_multimaps are equal

示例 2

让我们看下面的例子,我们将对相同类型但元素不同的 multimap 使用 operator!=() 函数。

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

输出

如果我们运行上面的代码,它将生成以下输出:

Both unordered_multimaps are not equal

示例 3

考虑以下示例,我们将使用 operator!=() 函数来显示 multimap 的元素(如果它们不相等),否则如果它们相等,则显示任何一个 multimap 的元素。

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

输出

以下是上述代码的输出:

elements of umm1
vivek : verma
akash : gupta
aman : kumar
aman : kumar

elements of umm2
Akash : Kumar
Alok : Gupta
Sakshi : Sharma
Sakshi : Sharma

示例 4

以下是一个示例,我们将使用 operator!=() 函数并以布尔值显示输出。

Open Compiler
#include <iostream> #include <unordered_map> using namespace std; int main() { unordered_multimap<string, string> umm1 = {{"Aman", "kumar"}, {"akash", "gupta"}, {"Alok", "gupta"}, {"Aman", "yadav"}}; unordered_multimap<string, string> umm2 = {{"Aman", "kumar"}, {"akash", "gupta"}, {"Alok", "gupta"}, {"Aman", "yadav"}}; cout<<boolalpha; cout<<(umm1 != umm2)<<endl; cout<<(!(umm1 != umm2))<<endl; return 0; }

输出

上述代码的输出如下:

false
true
广告