C++ forward_list 庫 - empty() 函式



描述

C++ 函式std::forward_list::empty() 測試 forward_list 是否為空。大小為零的 forward_list 被認為是空的。

宣告

以下是來自 std::forward_list 標頭檔案的 std::forward_list::empty() 函式宣告。

C++11

bool empty() const noexcept;

引數

返回值

如果 forward_list 為空則返回 true,否則返回 false。

異常

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

時間複雜度

常數時間,即 O(1)

示例

以下示例演示了 std::forward_list::empty() 函式的用法。

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {
   forward_list<int> fl;

   if (fl.empty())
      cout << "List is empty." << endl;

   fl.emplace_front(1);

   if (!fl.empty())
      cout << "List is not empty." << endl;

   return 0;
}

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

List is empty.
List is not empty.
forward_list.htm
廣告
© . All rights reserved.