C ++ STL中的std :: list :: empty()函数
empty()是列表类的函数,用于检查列表容器是否为空,如果列表容器为空即大小为0,则返回true(整数值:1),否则返回false(整数)值:0)。
语法:
list::empty(void);
参数:没有参数传递给函数
返回类型:
如果列表容器为空,则为真(1)
假,如果列表容器不为空
示例
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范例1:
在此示例中,有两个列表,list1具有5个元素,list2具有0个元素。我们必须检查列表容器是否为空?
#include <iostream>
#include <list>
using namespace std;
int main() {
//声明和初始化列表
list<int> list1 {10, 20, 30, 40, 50};
list<int> list2;
//检查list1是否为空
if(list1.empty())
cout<<"list1 is an empty list\n";
else
cout<<"list1 is not an empty list\n";
//检查list2是否为空
if(list2.empty())
cout<<"list2 is an empty list\n";
else
cout<<"list2 is not an empty list\n";
return 0;
}输出结果
list1 is not an empty list
list2 is an empty list热门推荐
10 对当兵的祝福语简短
11 同事贺卡祝福语简短冬日
12 花店写母亲祝福语简短
13 新年祝福语给长辈简短
14 烟酒店开业祝福语简短
15 新年儿童祝福语大全 简短
16 立春幽默祝福语简短
17 新年喝酒祝福语大全简短
18 爸爸爱打牌祝福语简短