How to clear a vector in C++?
Tags: cpp, oop, programming
Published: 2020-12-17 18:59:26

Vectors are very similar to the dynamic arrays but they have the ability to resize itself automatically whenever an element is inserted or deleted. Their storage is being handled by the container automatically.

In this article we are going to discuss the different methods, approach and also the program itself to clear a vector in C++

---
title: How to clear a vector in C++?
tags: cpp
canonical_url: https://kodlogs.com/blog/643/how-to-clear-a-vector-in-c
---


#Methods
There are two ways of clearing a vector in C++

* Using vector::clear()
* vector::erase()

But before going into deep discussion of how to use these two methods, we should know when to use what?

**clear()**

The function clear() makes the size of the vector to 0 by removing all the elements from a vector container. And all the work of removing elements is done by clear() function.

**erase()**

If we want to remove the specific elements from the container or a specific range of elements then we can use the function erase() and that all about erase() that how it reduces the size of the vector.

##Using vector::clear()
Now , we all know that the function clear() is used to clear or remove all the elements of the vector and reduce the size of the vector to 0. The syntax of using clear() is like this 
`
vectorname.clear()
Parameters :
No parameters are passed.
Result :
All the elements of the vector are
removed ( or destroyed )
`
**Example** 
`
Input  : myvector= {1, 2, 3, 4, 5};
         myvector.clear();
Output : myvector= {}

Input  : myvector= {};
         myvector.clear();
Output : myvector= {}
`
**Note : vector::clear() shows error whenever a parameter is passed.** For example
```
// CPP program to illustrate 
// Implementation of clear() function 
#include <iostream> 
#include <vector> 
using namespace std; 
  
int main() 
{ 
    vector<int> myvector; 
    myvector.push_back(1); 
    myvector.push_back(2); 
    myvector.push_back(3); 
    myvector.push_back(4); 
    myvector.push_back(5); 
  
    // Vector becomes 1, 2, 3, 4, 5 
  
    myvector.clear(); 
    // vector becomes empty 
  
    // Printing the vector 
    for (auto it = myvector.begin(); it != myvector.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
}
``` 
**Output**

No Output
##Using vector::erase()
If we want to remove specific elements or the specific range of the elements then we have to use the function vector::erase() and the syntax for using the erase() is like this 
`
1. vectorname.erase(position)
2. vectorname.erase(startingposition, endingposition)
Parameters :
Position of the element to be removed in the form of iterator.
or the range specified using start and end iterator.
Result :
Elements are removed from the specified
position of the container.
`
**Example** 
`
Input  : myvector= {1, 2, 3, 4, 5}, iterator= 2
         myvector.erase(iterator);
Output : 1, 2, 4, 5

Input  : myvector= {1, 2, 3, 4, 5, 6, 7, 8}, iterator1= 3, iterator2= 6
         myvector.erase(iterator1, iterator2);
`
**Output : 1, 2, 3, 7, 8**
**Note :** The only exception is that we have to use the valid position for removing elements. Otherwise it shows undefined behaviour.

 ###Programs
```
♦ Removing the element from the particular position
// CPP program to illustrate 
// working of erase() function 
#include <iostream> 
#include <vector> 
using namespace std; 
  
int main() 
{ 
    vector<int> myvector{ 1, 2, 3, 4, 5 }; 
    vector<int>::iterator it; 
  
    it = myvector.begin(); 
    myvector.erase(it); 
  
    // Printing the Vector 
    for (auto it = myvector.begin(); it != myvector.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
} 
``` 

**Output**
2 3 4 5
♦ ###Removing elements within a specific range
```
// CPP program to illustrate 
// Implementation of erase() function 
#include <iostream> 
#include <vector> 
using namespace std; 
  
int main() 
{ 
    vector<int> myvector{ 1, 2, 3, 4, 5 }; 
    vector<int>::iterator it1, it2; 
  
    it1 = myvector.begin(); 
    it2 = myvector.end(); 
    it2--; 
    it2--; 
  
    myvector.erase(it1, it2); 
  
    // Printing the Vector 
    for (auto it = myvector.begin(); it != myvector.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
} 
```
Output

4 5