C++ std::vector empty() Function

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

Definition

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

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 vector, add an element, and check again.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v;

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

    v.push_back(42);

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

How It Works

  1. A brand new vector 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 Vector Is Empty

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

C++
#include <vector>
#include <iostream>

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

    while (!v.empty()) {
        sum += v.back();
        v.pop_back();
    }

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

How It Works

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

Example 3 — Guard front() with empty()

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

C++
#include <vector>
#include <iostream>

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

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

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

How It Works

  1. showFront() first asks v.empty(). If the answer is true, it prints a message and returns early.
  2. For the empty vector 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 vector 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 vector, but empty() is clearer and is guaranteed to be constant time for every container.
front(), back(), and pop_back() on an empty vector are undefined behavior. A quick empty() check keeps the program safe.
Yes. std::vector<int> d; has no elements, so d.empty() is true.

Next: end()

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

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.

13 people found this page helpful