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.
size_type erase (const value_type& valueOfElementToBeDeleted);
Below is the implementation of the above approach:
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.
- Passing by pointer Vs Passing by Reference in C++
- Parameter Passing Techniques in C/C++
- Passing vector to a function in C++
- Passing a vector to constructor in C++
- Passing Reference to a Pointer in C++
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Passing and Returning Objects in C++
- How to delete last element from a set in C++
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- How to delete last element from a map in C++
- How to delete last element from a List in C++ STL
- Delete middle element of a stack
- How to delete a range of values from the Set using Iterator
- delete and free() in C++
- "delete this" in C++
- Minimum delete operations to make all elements of array same
- new and delete operators in C++ for dynamic memory
- Delete elements in C++ STL list
- Different ways to delete elements in std::map (erase() and clear())
- Overloading New and Delete operator in c++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29