Which Method Removes All Elements From Vector in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

We can remove all the elements of a vector using different methods present in the C++ Standard Template Library (STL). The methods used for removing the elements from a vector are:

  • vector::pop_back()
  • vector::pop_front()
  • vector::erase()
  • vector::clear()
  • remove(first, last, val)
  • remove_if()
  • remove_copy(first, last, result, val) Let us discuss each of the above methods to remove the elements of a vector in detail.

Method 1: vector::pop_back()

The method vector::pop_back() is a method that removes the elements stored in a vector one by one from the end. It removes the element and reduces the size of a container by one.

Syntax

Parameters: NONE
Return Value: NONE

Example

In the above example, we have created a vector v with the elements 63, 12, 21, 42, 56, and 98. Then, we use the pop_back() function to remove elements from the end of the vector. If we want to remove all the elements of a vector, we pop them one by one by using for loop. The size of the vector keeps on decreasing simultaneously.

Output

Time Complexity

The time complexity of the above function is O(n) as the statement inside the loop runs n number of times.

Method 2: vector::erase()

The method vector::erase() is a method that helps us remove a single element or a range of elements from a vector in C++ STL. It destroys the deleted element and simultaneously decreases the size of the container by the number of elements removed.

Parameters

position: The Position parameter is an iterator pointing to the single element that needs to get removed from the vector.

first, last: The first and last parameters are the iterator that specifies the range of elements within the vector that is to remove. The range includes all the elements between the first and the last pointer. It considers the element pointed by the first parameter but excludes the element pointed by the last.

Return Value

The return value is an iterator pointing to the location following the last erased element during the function call. If an operation erases all the vector elements, the iterator pointing to the end of the vector gets returned.

Example

Let us first see how the erase function works.

In the above example, we have created a vector called example_vector with elements ranging from 1 to 10. Then, we used the erase function with the position parameter to remove elements from a specific position. Since, vectors use 0-based indexing, we have to add a parameter that is one less than the actual one. To erase the 6th element from the vector, we have taken the position to be example_vector.begin() + 5. We can also use the erase function with the first and last parameters to erase a range of elements. Here, we have erased the elements ranging from example_vector.begin() to example_vector.begin() + 3. The elements at the index 0 to 2 get erased as we exclude the element at the 3rd index. So, now the vector is 4, 5, 7, 8, 9, 10.

The size of the vector keeps on decreasing simultaneously.

Output

I hope you have understood the basics of the erase function. Let us now see how to remove all the elements of a vector using the erase method.

First, we will use the erase(position) method to remove all the elements of a vector.

Example - erase(position)

In the above example, we have created an example vector v with elements ranging from 1 to 10. Then, we use the erase function with the position parameter to remove elements from a specific position. We can remove all the elements of a vector by using the erase function with a for loop. The last position of a vector gets passed to the erase function as a parameter. It erases all the elements in the vector from the end, one by one.

Output

We can also remove all the elements of a vector using the erase method by putting the first and last pointers as parameters.

Example - erase(first, last)

Here, we use the erase function with the first and last parameters to remove elements from a specific range. We can remove all the elements of a vector using the erase function with the first parameter at the beginning and the last parameter at the end of the vector.

Output

Method 3: clear()

Syntax

Parameters

No Parameters

Time Complexity

The time complexity to remove all the elements using the clear() function is O(N).

Example

In the above example, we have created a vector temp with elements ranging from 1 to 10. Then, we used the clear() function to remove all elements of a vector.

Output

Method 4: remove(first, last, value)

The method remove(first, last, value) deletes all the elements in the vector which are equal to the value and lies in the range of the first and the last iterator. It returns an iterator pointing to the new end of the vector formed.

Syntax

Parameters

first, last: The first and the last parameters are iterators demonstrating the start and end of the vector to consider for deletion of values. The range includes the element at first and excludes the last.

value: It is the value that gets deleted from the given range of elements.

Return value

The method returns an iterator pointing to the last element of the vector.

Example

In the above example, we have created a vector v with elements 20, 10, 30, 20, 40, 60, 20, and 10. Then, we use the remove function with the first and last parameter pointing to the start and end of the vector and the value to delete as 20. The remove function removes all the occurrences of 20 from the vector by shifting the other elements. Thus, the size of the vector remains the same as the element does not get removed. They move to a previous index. We still have extraneous elements at the end of the vector which are exactly the elements stored there (at that index) beforehand. These elements are to be removed.

We use the erase function to remove the extra elements not removed by the remove function. The first parameter takes the iterator returned by the remove function, while the last parameter takes the end of the vector. Thus, the final vector after removing the element 20 is 10, 30, 40, 60, 10.

Output

Method 5: remove_if()

The method remove_if() takes the first, last, and function as a parameter. It removes all the elements from the range [first, last) for which the given function returns true.

remove_if() does not remove the elements from the container. It shifts the element to the front if it is not needed and returns the iterator pointing to the new end. We can use erase function to remove the extra unnecessary space in the vector.

Syntax

Parameters

first, last: The first and the last parameters are iterators demonstrating the start and end of the vector to consider for deletion of values. The range includes the element at first pointer and excludes the last.

func: The func is a function parameter taking all the values in the range as arguments and returns a value convertible to a boolean. If the value returned by the function is true, the element gets removed from the vector.

Example

In the above example, we have created a vector v with elements from 1 to 10. Then, we use the remove_if() function with the first, last, and func parameter. The first and last parameter takes the iterator pointing to the start and end of the vector. The func parameter takes the function that decides whether the number should be deleted or not. Here, isEven() is the function that deletes all the even numbers in the array. The remove_if() method shifts other numbers to the position of even numbers but does not delete them. We use the erase function to remove the unnecessary space by passing the iterator returned by the remove_if() method as the first parameter and the end of the vector as the last parameter. The final vector obtained using the remove and erase function is 1, 3, 5, 7, 9.

Output

Method 6: remove_copy(first, last, result, value)

The remove_copy() is a method that copies the elements in the given range to a new vector leaving those equal to the value specified.

Syntax

Parameters

first, last: The first and the last parameters are iterators demonstrating the start and end of the vector to consider for the deletion of values. The range includes the element at first pointer and excludes the last.

result: It is a vector in which the removed element gets copied.

value: It is the value that needs to be removed.

Example

In the above example, we have created a vector v1 with elements 10, 20, 10, 30, 20, 20, 30, 10 and another vector v2 of the same size as v1. Then, we use the remove_copy() function with the first, last, result, and value parameters. The first and last parameter takes the iterator pointing to the start and end of the vector v1. The result parameter takes the vector in which the result gets copied. The value parameter takes the value that we need to remove. Thus the final vector contains 10, 10, 30, 30, 10, 0, 0, 0.

Output

Another Example for a Better Understanding of the Above Methods

We hope you have got your answer to the question - "Which method is used to remove all elements from a vector"?

Now, let us see one more example that will help you get better understanding of the concept.

Learn More About Vectors in C++ in Detail

You can learn more about vectors in C++ at Scaler Topics.

Conclusion

  • In this article, we learned which method is used to remove all elements from vectors.
  • Vectors are containers that store elements having the ability to resize themselves automatically upon insertion and deletion of a new element.
  • We can remove elements from a vector from a given position or a specified range.
  • We use the erase and remove method with different parameters to remove all or some specific elements from a vector.
  • The clear() and erase() methods have time complexities of O(N) as they operate in linear time.
  • remove_copy() copies the elements of a vector to another and thus has a space complexity of O(N).