Cpp-standard-library-cpp-array-empty
提供:Dev Guides
C ++配列ライブラリ-empty()関数
説明
- C ++関数* std
- array :: empty()*は、配列のサイズがゼロかどうかをテストします。
宣言
- 以下は、std
- arrayヘッダーからのstd :: array :: empty()関数の宣言です。
constexpr bool empty() noexcept;
パラメーター
None
戻り値
配列サイズが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