Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ empty function

C++ empty function

C++ Container Class The C++ container class


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


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 个元素


C++ Container Class The C++ container class