Vector Clear Function - C++ Programming Tutorial
C++ Course / STL Vector / Vector Clear Function

Vector Clear Function

BLUF: Mastering Vector Clear Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Vector Clear Function

C++ is renowned for its efficiency. Learn how Vector Clear Function enables low-level control and high-performance computing in the tutorial below.

C++ Vector clear

This function eliminates all elements from the vector.

Syntax

Consider a vector v. Syntax would be:

Example

v.clear();

Parameter

It does not contain any parameter.

Return value

It does not return any value.

Example

Let's see a simple example.

Example

#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int>  v{1,2,3,4,5};
cout<<"Elements of v vector are :";
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
v.clear();
for(int i=0;i<v.size();i++)
cout<<v[i];
	return 0;
}

Output:

Output

Elements of v vector are :1 2 3 4 5

In this instance, the clear method eliminates all elements within the vector.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience