English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ vector clear() Usage and Example

C++ Vector (Container)

This function removes all elements from the vector.

Syntax

Vector (vector) v. Syntax is:

v.clear();

Parameter

It does not contain any parameters.

Return Value

It does not return any value.

Online Example

Remove all elements from the vector.

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

Output Result:

The elements of vector v are:1 2 3 4 5

In this example, the clear() function removes all elements from the vector (vector).

C++ Vector (Container)