stl::list_C ++ STL中的std :: list :: empty()函数

stl::list

empty() is the function of list class, it is used to check whether a list container is empty or not, it returns true (integer value: 1) if list container is empty i.e. its size is 0, otherwise function return false (integer value: 0).

empty()是列表类的函数,用于检查列表容器是否为空,如果列表容器为空即大小为0,则返回true (整数值:1),否则返回false (整数)值:0)。

Syntax:

句法:

list::empty(void);

Parameter: No parameter is passed to the function

参数:没有参数传递给函数

Return type:

返回类型:

  • True (1), if list container is empty

    如果列表容器为空,则为真(1)

  • False (0), if list container is not empty

    假(0),如果列表容器不为空

Example:

例:

    Input: list list1 {10, 20, 30, 40, 50}
    Function calling/validation: list1.empty();
    Output: False

    Input: list list1 {}
    Function calling/validation: list1.empty();
    Output: True

Example 1:

范例1:

In this example, there are two lists, list1 has 5 elements and list2 has 0 elements. We have to check whether list containers are empty or not?

在此示例中,有两个列表,列表1具有5个元素,列表2具有0个元素。 我们必须检查列表容器是否为空?

#include <iostream>
#include <list>
using namespace std;

int main() 
{
	//declare and initialize lists
	list<int> list1 {10, 20, 30, 40, 50};
	list<int> list2;

	//check list1 is empty or not
	if(list1.empty())
		cout<<"list1 is an empty list\n";
	else
		cout<<"list1 is not an empty list\n";

	//check list2 is empty or not
	if(list2.empty())
		cout<<"list2 is an empty list\n";
	else
		cout<<"list2 is not an empty list\n";

	return 0;
}

Output

输出量

    list1 is not an empty list
    list2 is an empty list


Example 2:

范例2:

In this example, there is one list with 5 elements, we have to print its elements by checking till list is not empty i.e. we have to print all elements, and also check if list is empty then returns false.

在此示例中,有一个包含5个元素的列表,我们必须通过检查直到list不为空来打印其元素,即我们必须打印所有元素,还要检查list是否为空然后返回false。

#include <iostream>
#include <list>
using namespace std;

int main () 
{
	//declaring list
	list<int> list1 {10, 20, 30, 40, 50};

	//printing the elements, if list1 is not empty
	if( !(list1.empty()) )
	{
		cout<<"List's elements are:\n";
		while(!(list1.empty()))
		{
			cout<<list1.front()<<endl;
			list1.pop_front();
		}
	}	
	else
		cout<<"list is empty!!!\n";

	return 0;
}

Output

输出量

    List's elements are:
    10
    20
    30
    40
    50


翻译自: https://www.includehelp.com/stl/std-list-empty-function.aspx

stl::list

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值