C++ multimap :: operator!=() 函数



C++ 的 std::multimap::operator!=() 用于检查两个 multimap 是否不相等。它比较两个 multimap 中的元素及其数量。如果它们不同,则返回 true,否则返回 false。此函数确保两个 multimap 在键值对和出现次数方面不完全相同。

语法

以下是 std::multimap::operator!=() 函数的语法。

bool operator!=( const std::multimap<Key, T, Compare, Alloc>& lhs, const std::multimap<Key, T, Compare, Alloc>& rhs );

参数

  • lhs - 表示第一个 multimap 对象。
  • rhs - 表示第二个 multimap 对象

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

返回值

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

异常

此函数不抛出异常。

时间复杂度

此函数的时间复杂度为线性,即 O(n)

示例

让我们看下面的例子,我们将演示 operator!=() 函数的使用。

Open Compiler
#include <iostream> #include <map> int main() { std::multimap<int, std::string> a; std::multimap<int, std::string> b; a.insert({1, "Hi"}); b.insert({1, "Hi"}); if (a != b) { std::cout << "Multimaps are not equal" << std::endl; } else { std::cout << "Multimaps are equal" << std::endl; } return 0; }

输出

以上代码的输出如下:

Multimaps are equal

示例

考虑以下示例,我们将具有相同数量的元素,但键不同。

Open Compiler
#include <iostream> #include <map> int main() { std::multimap<int, std::string> a; std::multimap<int, std::string> b; a.insert({1, "Hi"}); b.insert({3, "Hi"}); if (a != b) { std::cout << "Multimaps are not equal" << std::endl; } else { std::cout << "Multimaps are equal" << std::endl; } return 0; }

输出

以下是以上代码的输出:

Multimaps are not equal

示例

在以下示例中,我们将比较具有不同元素的 multimap 并观察输出。

Open Compiler
#include <iostream> #include <map> int main() { std::multimap<int, std::string> a; std::multimap<int, std::string> b; a.insert({1, "Hi"}); b.insert({1, "Hello"}); if (a != b) { std::cout << "Multimaps are not equal." << std::endl; } else { std::cout << "Multimaps are equal." << std::endl; } return 0; }

输出

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

Multimaps are not equal.
multimap.htm
广告