Checking if a vector is empty

To check if a vector is empty, you can use the empty() method provided by most standard library implementations. This method returns a boolean value indicating whether the vector is empty or not.

Let’s take a look at some example code in Python, C++, and Java to demonstrate how to check if a vector is empty:

Python

my_vector = []  # An empty list
if len(my_vector) == 0:
    print("Vector is empty")
else:
    print("Vector is not empty")

C++

#include <iostream>
#include <vector>

int main() {
    std::vector<int> my_vector;  // An empty vector
    if (my_vector.empty()) {
        std::cout << "Vector is empty" << std::endl;
    } else {
        std::cout << "Vector is not empty" << std::endl;
    }
    return 0;
}

Java

import java.util.Vector;

public class Main {
    public static void main(String[] args) {
        Vector<Integer> myVector = new Vector<>();  // An empty vector
        if (myVector.isEmpty()) {
            System.out.println("Vector is empty");
        } else {
            System.out.println("Vector is not empty");
        }
    }
}

In all three examples, we create an empty vector named my_vector or myVector and check if it is empty using the len() in Python, empty() in C++, or isEmpty() in Java. We then print the appropriate message based on the result.

By checking if a vector is empty before performing operations or accessing its elements, you can ensure your code handles empty vectors gracefully, avoiding potential errors.