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