stl中empty_C ++ STL中的array :: empty()示例

C++ STL中的`array`是一个高效、轻量级的固定大小数组容器。`array::empty()`函数用于检查数组是否为空,当数组大小为0时返回`true`,否则返回`false`。示例代码展示了如何使用该函数。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值