VOOZH about

URL: https://www.geeksforgeeks.org/cpp/vector-clear-in-cpp-stl/

⇱ Vector clear() in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Vector clear() in C++ STL

Last Updated : 23 Jul, 2025

In C++, vector clear() is a built-in method used to remove all elements from a vector, making it empty. In this article, we will learn about the vector clear() method in C++.

Let’s take a look at an example that illustrates the vector clear() method:


Output
Vector is Empty

This article covers the syntax, usage, and common examples of the vector clear() method in C++ STL.

Syntax of Vector clear()

The vector clear() method is defined inside the std::vector class in the <vector> header file.

v.clear();

This function does not take any parameters, nor it returns any value.

Examples of Vector clear()

The below examples demonstrate the use and behaviour of vector clear() method in practical scenarios.

Delete All the Elements of a Vector


Output
0

Effect of Vector clear() on the Capacity


Output
4
4

Explanation: As we can see, the vector clear() method does not affects the capacity of the vector. It means that although it deletes the memory

Comment