C++ STL中的列表EMPTY()函数

这个 list::empty() C++中的内置函数用于检查特定的列表容器是否为空。此函数不修改列表,只检查列表是否为空,即列表大小是否为零。

null

语法:

list_name.empty() 

参数: 此函数不接受任何参数,它只是检查列表容器是否为空。

返回值: 此函数的返回类型为 布尔值 .它回来了 符合事实的 列表容器的大小为零,否则返回 错误的 .

下面的程序演示了list::empty()函数。

// CPP program to illustrate the
// list::empty() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Creating a list
list< int > demoList;
// check if list is empty
if (demoList.empty())
cout << "Empty List" ;
else
cout << "Not Empty" ;
// Add elements to the List
demoList.push_back(10);
demoList.push_back(20);
demoList.push_back(30);
demoList.push_back(40);
// check again if list is empty
if (demoList.empty())
cout << "Empty List" ;
else
cout << "Not Empty" ;
return 0;
}


输出:

Empty List
Not Empty

笔记 :此函数在恒定的时间复杂度下工作。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享