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 教师闺蜜祝福语简短