當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Array empty()用法及代碼示例


描述

C++ 函數std::array::empty()測試數組的大小是否為零。

聲明

以下是 std::array::empty() 函數形式 std::array 標頭的聲明。

constexpr bool empty() noexcept;

參數

返回值

如果數組大小為 0,則返回 true,否則返回 false。

異常

此成員函數從不拋出異常。

時間複雜度

常數,即 O(1)

示例

在下麵的示例中,arr1 的大小為 0,這就是為什麽它將被視為空數組並且成員函數將為 arr1 返回真值。

#include <iostream>
#include <array>

using namespace std;

int main(void) {
   
   /* array size is zero, it will be treated as empty array */
   array<int, 0> arr1;   
   array<int, 10> arr2;

   if (arr1.empty())
      cout << "arr1 is empty" << endl;
   else
      cout << "arr1 is not empty" << endl;

   if (arr2.empty())
      cout << "arr2 is empty" << endl;
   else
      cout << "arr2 is not empty" << endl;
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

arr1 is empty
arr2 is not empty

相關用法


注:本文由純淨天空篩選整理自 C++ Array Library - empty() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。