C++ empty function
empty is used in list tochecks whether the linked list is empty.
empty is a member function of the container class that returns whether the linked list is empty.
This is the most commonly used function for checking the linked list's state, and it is more efficient than size() == 0 .
Word meaning: empty means "empty," i.e., to check whether the linked list is empty.
Basic syntax and parameters
Syntax format
bool empty() const;
Function description
- Return Value: If the linked list is empty, returns
true; otherwise, returnsfalse.
Example
Example 1: Check whether the linked list is empty
Example
#include
#include
void
processList
(
const
std
::
list
<
int
&
lst
)
{
if
(lst.
empty
(
)
)
{
std
::
cout
<<
"Linked list is empty"
<<
std
::
endl
;
}
else
{
std
::
cout
<<
"Linked list has "
<<
lst.
size
(
)
<<
" elements"
<<
std
::
endl
;
}
}
int
main
(
)
{
std
::
list
<
int
emptyList;
processList
(
emptyList
)
;
std
::
list
<
int
numbers
=
{
1
,
2
,
3
};
processList
(
numbers
)
;
return
0
;
}
Expected output:
链表为空
链表有 3 个元素
The C++ container class