C++ 列表庫 - operator== 函式



描述

C++ 函式std::list::operator== 用於測試兩個列表是否相等。

宣告

以下是來自 std::list 標頭檔案的 std::list::operator== 函式的宣告。

C++98

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

引數

  • first - 第一個列表物件。

  • 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
list.htm
廣告