In C++, std::list::empty() tells you whether a std::list has no elements. It returns true for an empty list and false otherwise.
It does not change the list. Use it before calling front(), back(), pop_front(), or pop_back(). Include <list> to use it.
This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax
Call empty with empty brackets. It returns a bool.
C++
bool e = l.empty(); // true when there are no elements
if (!l.empty()) {
l.pop_back();
}
Call
What it does
l.empty()
Returns true if the list has no elements, otherwise false.
l.empty() says the same as l.size() == 0, but it states your intention more clearly, so most programmers prefer it.
Library declarations
C++
bool empty() const noexcept;
Parameters
Parameter
Description
None
empty() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
bool
true if the list has no elements, false if it has at least one.
C++ Examples
Example 1 — empty() Before and After Adding
A new list is empty. After one push_back(), it is not.