In C++, std::vector::empty() tells you whether a std::vector has no elements. It returns true for an empty vector and false otherwise.
It is the safe way to guard calls such as front(), back(), pop_back(), and pop_back(). Include <vector> to use it.
This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.
Syntax
Call empty with empty brackets. It takes no arguments.
C++
if (v.empty()) { /* nothing inside */ }
while (!v.empty()) { /* process items */ }
Call
What it does
v.empty()
Returns true if the vector has no elements.
!v.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 vector, add an element, and check again.