YetiCodeCamp - C++ for Beginners

Companion website for the C++ for Beginners YouTube video series.

C++ Vector empty() Function

The C++ vector.empty() function returns boolean false if the vector is empty, otherwise returns true if the vector has at least one element. This function does not take any parameters.


Example, using a vector “v1”.


Returns boolean false if v1 is empty, otherwise returns true.

std::vector<int> v1;
std::cout << v1.empty();

Output: 1

This example is a complete program using the vector empty() function to determine if a vector is empty.


void prtEmpty(const std::vector<auto> &v, std::string n="")
  if (v.empty())
       std::cout << "Vector " << n << " is empty.\n";
       std::cout << "Vector " << n << " is not empty.\n";

int main()
std::vector<int> v1;
std::vector<int> v2 { 1,2,3};

prtEmpty(v1, "v1");
prtEmpty(v2, "v2");

Vector v1 is empty.
Vector v2 is not empty.