C++ 列表库 - 运算符 == 函数


描述

C++ 函数std::list::operator==测试两个列表是否相等。

宣言

以下是 std::list::operator== 函数形式 std::list 标头的声明。

C++98

template <class T, class Alloc>
bool operator== (const list<T,Alloc>& first, const list<T,Alloc>& second);

参数

  • 第一个- 第一个列表对象。

  • 第二个- 相同类型的第二个列表对象。

返回值

如果第一个列表与第二个列表相同,则返回 true,否则返回 false。

例外情况

这个函数永远不会抛出异常。

时间复杂度

线性即 O(n)

例子

以下示例显示了 std::list::operator== 函数的用法。

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l1 = {1, 2, 3};
   list<int> l2 = {1, 2, 3};

   if (l1 == l2)
      cout << "List l1 and l2 are equal" << endl;

   l1.push_back(4);

   if (!(l1 == l2))
      cout << "List l1 and l2 are not equal" << endl;

   return 0;
}

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

List l1 and l2 are equal
List l1 and l2 are not equal
列表.htm