In this C++ tutorial, you will learn how to check if a vector is empty using vector::empty() function, with example program.

C++ Check if Vector is Empty

empty() function checks if this vector is empty or not.

empty() returns true if the vector is empty, or false if the vector is not empty.

Example

In the following C++ program, we define an empty vector of integers, and programmatically check if this vector is empty or not using empty() function.

main.cpp

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums;
    
    if (nums.empty()) {
        cout << "Vector is empty." << endl;
    } else {
        cout << "Vector is not empty." << endl;
    }
}

Output

Vector is empty.

Conclusion

In this C++ Tutorial, we learned how to check if vector is empty or not using empty() function.