stl中empty
"array" is a container which is used to create/container which is used to create/contains the fixed size arrays, the "array" in C++ STL is "class" actually and they are more efficient, lightweight and very easy to use, understand, "array" class contains many inbuilt functions, thus the implementation of the operations are fast by using this rather that C-Style arrays.
“数组”是用于创建/包含用于创建/包含固定大小的数组的容器,C ++ STL中的“数组 ”实际上是“类” ,它们更高效,轻巧且非常易于使用,可以理解, “数组”类包含许多内置函数,因此使用此数组而不是C-Style数组可以快速实现操作。
To use "array" class and its function, we need to include "array" class and its function, we need to include "array" header.
要使用“数组”类及其功能,我们需要包括“数组”类及其功能,我们需要包括“数组”标头。
array :: empty()函数 (array::empty() function)
empty() function is used to check whether an array is empty or not. It returns 1 (true), if the array size is 0 and returns 0 (false), if array size is not zero.
empty()函数用于检查数组是否为空。 如果数组大小为0,则返回1(真),如果数组大小不为零,则返回0(假)。
Syntax:
句法:
array_name.empty();
Parameters:
参数:
There is no parameter to be passed.
没有要传递的参数。
Return type:
返回类型:
1 (true) – if array is empty
1(true)–如果数组为空
0 (false) – if array is not empty
0(假)–如果数组不为空
Example:
例:
Input:
arr1{} //an empty array
arr2{10,20,30} //array with 3 elements
Function calls:
arr1.empty()
arr2.empty()
Output:
1
0
Program:
程序:
#include <iostream>
#include <array>
using namespace std;
int main() {
//declaring two arrays
array<int,0> arr1{};
array<int,5> arr2{};
array<int,5> arr3{10, 20, 30};
array<int,5> arr4{10, 20, 30, 40, 50};
//printing arr_name.empty() value
cout<<"arr1.empty(): "<<arr1.empty()<<endl;
cout<<"arr2.empty(): "<<arr2.empty()<<endl;
cout<<"arr3.empty(): "<<arr3.empty()<<endl;
cout<<"arr4.empty(): "<<arr4.empty()<<endl;
//checking and printing messages
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;
if(arr3.empty())
cout<<"arr3 is empty"<<endl;
else
cout<<"arr3 is not empty"<<endl;
if(arr4.empty())
cout<<"arr4 is empty"<<endl;
else
cout<<"arr4 is not empty"<<endl;
return 0;
}
Output
输出量
arr1.empty(): 1
arr2.empty(): 0
arr3.empty(): 0
arr4.empty(): 0
arr1 is empty
arr2 is not empty
arr3 is not empty
arr4 is not empty
翻译自: https://www.includehelp.com/stl/array-empty-in-cpp-stl-with-example.aspx
stl中empty