C++ std::deque empty() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::deque

Definition

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 */ }
CallWhat 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

ParameterDescription
Noneempty() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
booltrue if size() == 0, otherwise false.

C++ Examples

Example 1 — Basic empty()

Check a new deque, add an element, and check again.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d;

    std::cout << std::boolalpha;
    std::cout << "empty at start: " << d.empty() << '\n';

    d.push_back(42);

    std::cout << "empty after push: " << d.empty() << '\n';
    return 0;
}

How It Works

  1. A brand new deque holds nothing, so empty() is true.
  2. push_back(42) adds one element.
  3. Now empty() is false. std::boolalpha prints the words instead of 1 and 0.

Example 2 — Loop Until the Deque Is Empty

Take numbers from the front one by one and add them up.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {10, 20, 30};
    int sum = 0;

    while (!d.empty()) {
        sum += d.front();
        d.pop_front();
    }

    std::cout << "sum: " << sum << '\n';
    std::cout << "left: " << d.size() << '\n';
    return 0;
}

How It Works

  1. The loop condition !d.empty() is true while at least one element is left.
  2. Each round adds front() to sum and then removes it with pop_front().
  3. After three rounds the deque is empty, the loop ends, and sum is 10 + 20 + 30 = 60.

Example 3 — Guard front() with empty()

Calling front() on an empty deque is undefined behavior, so check first.

C++
#include <deque>
#include <iostream>

void showFront(const std::deque<int>& d) {
    if (d.empty()) {
        std::cout << "empty deque: nothing to show\n";
        return;
    }
    std::cout << "front is " << d.front() << '\n';
}

int main() {
    std::deque<int> a;
    std::deque<int> b = {42, 43};

    showFront(a);
    showFront(b);
    return 0;
}

How It Works

  1. showFront() first asks d.empty(). If the answer is true, it prints a message and returns early.
  2. For the empty deque a, front() is never called, so there is no undefined behavior.
  3. For b, the check passes, and front() returns 42.

Frequently Asked Questions

It returns true if the deque has no elements and false if it has at least one.
No. empty() only checks. Use clear() to remove all elements.
Use empty(). Both give the same answer for a deque, but empty() is clearer and is guaranteed to be constant time for every container.
front(), back(), pop_front(), and pop_back() on an empty deque are undefined behavior. A quick empty() check keeps the program safe.
Yes. std::deque<int> d; has no elements, so d.empty() is true.

Next: end()

Get an iterator one place after the last element with std::deque.

end() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful