clear vs erase vector c++ code example

Example 1: clear vs erase vector c++

//Syntax
vectorname.clear()
vectorname.erase(startingposition, endingposition)
  
clear() removes all the elements from a vector container, thus making its 
size 0. All the elements of the vector are removed using clear() function. 
  
erase() function, on the other hand, is used to remove specific elements from 
the container or a range of elements from the container, thus reducing its
size by the number of elements removed.

Example 2: C++ Vector clear syntax

vector<T>().swap(x);   // clear x reallocating

Tags:

Cpp Example