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, 4};

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

   l2.pop_back();

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

   return 0;
}

讓我們編譯並執行上面的程式,這將產生以下結果:

List l1 and l2 are not equal
List l1 and l2 are equal
list.htm
廣告
© . All rights reserved.