In C++, std::deque::empty() tells you whether a std::deque has no elements. It returns true for an empty deque and false otherwise.
It is the safe way to guard calls such as front(), back(), pop_front(), and pop_back(). Include <deque> to use it.
This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.
Syntax
Call empty with empty brackets. It takes no arguments.
C++
if (d.empty()) { /* nothing inside */ }
while (!d.empty()) { /* process items */ }
Call
What it does
d.empty()
Returns true if the deque has no elements.
!d.empty()
Returns true if there is at least one element.
empty() only asks. To remove all elements, call clear(). Do not mix them up.
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 size() == 0, otherwise false.
C++ Examples
Example 1 — Basic empty()
Check a new deque, add an element, and check again.