Given a Set, the task is to remove the last element from this Set in C++.
Examples:
Input: set = [10 20 30 70 80 90 100 40 50 60], valueOfElementToBeDeleted = 100 Output: 10 20 30 40 50 60 70 80 90 Input: set = [1 2 3 4 5], valueOfElementToBeDeleted = 3 Output: 1 2 4 5
Sets are a type of associative containers in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.
Approach: In this method, the last element is deleted by using the erase function and calling it with the value of the last element as its argument. If the value of the last element is not known, then use the previous method.
Syntax:
size_type erase (const value_type& valueOfElementToBeDeleted);
Below is the implementation of the above approach:
C++
// 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 deleting " << valueOfElementToBeDeleted << ": " ; 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; } |
Python3
# Python3 program to delete an element of a # set by passing of a set by passing its value # function to print the set def printset(myset): for i in myset: print (i, end = " " ) print () # function to delete the element of a set def deleteByValue(myset, value): myset = sorted (myset) # printint original value print ( "Set orginally:" , end = " " ) printset(myset) # Erase the element that is equal to # the value passed as the parameter myset.remove(value) # printing all the elements of the set print ( "Set after deleting" , value, ":" , end = " " ) printset(myset) # Driver code myset = set () for i in range ( 1 , 10 ): myset.add(i * 10 ) # Get the valueOfElementToBeDeleted value = 50 # Delete an element from the Set deleteByValue(myset, value); # This code is contributed # by Mohit kumar 29 |
Set originally: 10 20 30 40 50 60 70 80 90 Set after deleting 50: 10 20 30 40 60 70 80 90
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.