C++ forward_list 庫 - operator< 函式



描述

C++ 函式std::forward_list::operator< 測試第一個 forward_list 是否小於另一個。

宣告

以下是來自 <forward_list> 標頭檔案的 std::forward_list::operator< 函式宣告。

C++11

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

引數

  • first − 第一個 forward_list 物件。

  • second − 第二個相同型別的 forward_list 物件。

返回值

如果第一個 forward_list 小於第二個,則返回 true,否則返回 false。

異常

此成員函式從不丟擲異常。

時間複雜度

線性,即 O(n)

示例

以下示例顯示了 std::forward_list::operator< 函式的使用方法。

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl1 = {1, 2, 3, 4};
   forward_list<int> fl2 = {1, 2, 3, 4, 5};

   if (fl1 < fl2)
      cout << "First list is less than second." << endl;

   fl1.push_front(5);
   fl1.push_front(6);
   fl1.push_front(7);

   if (!(fl1 < fl2))
      cout << "First list is not less than second." << endl;

   return 0;
}

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

First list is less than second.
First list is not less than second.
forward_list.htm
廣告
© . All rights reserved.