Delete an element from the Set by passing its value :
Approach :
In this method, an element is deleted by using the erase function and calling it with the value of that element as its argument. Syntax :
size_type erase (const value_type& valueOfElementToBeDeleted);
Below is the implementation of the above approach: Program 2:
// C++ program to delete an element
// of a Set by passing its value
#include <iostream>
#include <set>
using namespace std;
// Function to print the set
void printSet(set<int> myset)
{
// Get the iterator
set<int>::iterator it;
// printing all the elements of the set
for (it = myset.begin(); it != myset.end(); ++it)
cout << ' ' << *it;
cout << '\n';
}
// Function to delete the element of set
void deleteByValue (set<int> myset, int valueOfElementToBeDeleted)
{
// printing all the elements of the set
cout << "\nSet originally: ";
printSet(myset);
// Erase the element that is equal to
// the value passed as the parameter
myset.erase(valueOfElementToBeDeleted);
// printing all the elements of the set
cout << "Set after deletion: ";
printSet(myset);
}
// Driver code
int main()
{
set<int> myset;
// Get the set
for (int i = 1; i < 10; i++)
myset.insert(i * 10);
// Get the valueOfElementToBeDeleted
int valueOfElementToBeDeleted = 50;
// Delete an element from the Set
deleteByValue (myset, valueOfElementToBeDeleted);
return 0;
}
Delete a range of values from the Set using Iterator :
Approach :
In this method, a range of elements are deleted from the set. This is done with the help of two iterators. The first iterator points at the starting element of the range and the second iterator points at the last element of the range. The first iterator is inclusive while the last iterator is exclusive which means that the element pointed by the last iterator will not get deleted. Syntax :
iterator erase (const_iterator startPositionIterator_inclusive,
const_iterator endingPositionIterator_exclusive);
Below is the implementation of the above approach: Program:
// C++ program to delete an element
// of a Set by passing its value
#include <iostream>
#include <set>
using namespace std;
// Function to print the set
void printSet(set<int> myset)
{
// Get the iterator
set<int>::iterator it;
// printing all the elements of the set
for (it = myset.begin(); it != myset.end(); ++it)
cout << ' ' << *it;
cout << '\n';
}
// Function to delete the element of set
void deleteRange (set<int>& myset,
set<int>::iterator start_itr,
set<int>::iterator end_itr)
{
// printing all the elements of the set
cout << "\nSet originally: ";
printSet(myset);
// Erase the elements in the range
// of the iterators passed as the parameter
myset.erase(start_itr, end_itr);
// printing all the elements of the set
cout << "Set after deletion: ";
printSet(myset);
}
// Driver code
int main()
{
set<int> myset;
// Get the set
for (int i = 1; i < 10; i++)
myset.insert(i * 10);
// Get the starting Iterator at 3rd element
set<int>::iterator start_itr = ++(++myset.begin());
// Get the ending Iterator at 2nd last element
set<int>::iterator end_itr = --(--myset.end());
// Delete an element from the Set
deleteRange (myset, start_itr, end_itr);
return 0;
}