Vector erase() and clear() in C++ STL

Filed Under: C++
Vector Erase() And Clear() Method In C STL

In this article, we will be focusing on two important functions of Vector container provided by STL: erase() and clear().

These generic classes and functions are provided by the C++ Standard Template Library (STL) to manipulate and manage data. Let’s go over the vector clear() and vector erase() in C++ in detail.


Vector erase() in C++

The erase() function provided by Vector is basically used to delete elements at a specified position/index.

The erase() function allows users to delete the element at the specified index or a range of numbers within the specified range value of indexes.

Syntax:

vector.erase(index);
OR
vector.erase(start_index, end_index);

Example 1: Deleting an item at a specified index using erase() method

#include <iostream> 
#include <vector> 
using namespace std; 

int main() 
{ 
	vector<int> v = {10, 20, 30, 40, 50, 60}; 
	vector<int>::iterator x; 

	x = v.begin();
	x++;

	v.erase(x); 
	for (auto x = v.begin(); x != v.end(); ++x) 
		cout<< *x<<endl;
	return 0; 
} 

In the above snippet of code, we have set the iterator to the first element i.e. index 0. Then, we have incremented the position of the iterator using increment operator (++) to the second element i.e. index 1.

Finally, we have passed the updated value of the iterator as an argument to the vector erase function to delete the element at index 1 i.e. the second element (20).

Output:

10
30
40
50
60

Example 2: Deleting a set of items between the specified range of indexes.

#include <iostream> 
#include <vector> 
using namespace std; 

int main() 
{ 
	vector<int> v = { 10, 20, 30, 40, 50, 60 }; 
	vector<int>::iterator x, y; 

	x = v.begin();
	x++;
	y = v.end();
	y--;

	v.erase(x,y); 
	
	for (auto x = v.begin(); x != v.end(); ++x) 
		cout<< *x<<endl;
	return 0; 
} 

In the above piece of code, we have set two iterators x, y to point to the vector v. We have set the iterator ‘x’ to point to index 0 i.e. the first element of the vector and the iterator ‘y’ to point to the last element of the vector.

Further, we have incremented the iterator x to point to the second element i.e. 20 and decremented the iterator y to point to the second last element i.e. 50.

Finally, we have passed x and y as arguments to the erase() function to delete elements between the specified range.

Output:

10
60

Vector clear() method in C++

The vector.clear() function is used to flush or delete all the elements occupied by the vector. Thus, it makes the vector completely empty and the size of the vector turns out to be zero (0).

Syntax:

vector.clear();

Example:

#include <iostream> 
#include <vector> 
using namespace std; 

int main() 
{ 
	vector<int> v = { 10, 20, 30, 40, 50, 60 }; 
	
        v.clear();
    
	for (auto x = v.begin(); x != v.end(); ++x) 
		cout<< *x<<endl;
	return 0; 
} 

The above code does not yield any output, because the vector.clear() function deletes all the elements present in it.

Output:

No output

Conclusion

Thus, in this article, we have understood the working and basic difference between the vector.erase() method and vector.clear() method in C++ STL.

References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages