list::empty(是C++ STL中的内置函数,用于检查特定列表容器是否为空。该函数不会修改列表,它只是检查列表是否为空,即列表的大小是否为零。
用法:
list_name.empty()
参数:该函数不接受任何参数,仅检查列表容器是否为空。
返回值:该函数的返回类型为布尔值。如果列表容器的大小为零,则返回True,否则返回False。
以下示例程序旨在说明list::empty()函数。
// CPP program to illustrate the
// list::empty() function
#include
using namespace std;
int main()
{
// Creating a list
list demoList;
// check if list is empty
if (demoList.empty())
cout << "Empty List\n";
else
cout << "Not Empty\n";
// 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\n";
else
cout << "Not Empty\n";
return 0;
}
输出:
Empty List
Not Empty
注意:此功能以恒定的时间复杂度工作。