Insertion and Deletion in STL Set C++
Set is a container implemented in C++ language in STL and has a concept similar to how set is defined in mathematics. The facts that separates set from the other containers is that is it contains only the distinct elements and elements can be traversed in sorted order. Having the strong hold on sets is useful in competitive programming and solving algorithmic problems. The insertion and deletion in STL sets are discussed in this article.
- insert(ele) : This function inserts the element in set. The insertion only takes place when the element passed is not already in set. It returns a pointer pair . First element pointing to the element already present or newly inserted. Second element returning the boolean status “true” or “false”.
- insert(hint, ele) : In this implementation, the hint pointer is sent with the element to be inserted. The use of hint pointer is to help insert() know where the actual insertion has to take place. Hence, trying to reduce time to allocate the element. Hint pointer does not force the insertion at specific position. This function returns the pointer to the position where element is inserted.
- insert(beg_ptr, end_ptr) : This type of insertion is required to insert the elements of other container into set. The repeated elements are not inserted if they are present in the source container.
Using insert() : Insert function is used to insert the elements in the set. After insertion, the reordering of elements takes place and the set is sorted. This function is implemented in 3 ways.
The element was newly inserted The set elements after 1st insertion are : 20 The set elements after 2nd insertion are : 20 24 The set elements after 3rd insertion are : 20 24 25 26
- emplace() : Inserts element using in-place construction strategy. Increases the size of set by 1. returns a pointer pair. 1st element of which is iterator pointing to the position of inserted element. 2nd returns a boolean variable indicating an already present or newly created element.
- emplace_hint() : Takes a “hint_iterator” to get a hint of position of insertion to possibly reduce the time required to insert the element inserted. This does not effect the position of insertion. It takes place where it is defined to internally.
Using emplace : emplace is also used to insert the element into the Set. This function is similar to “insert()” discussed above, the only difference being that “in-place” construction of element takes place at the position of element insertion contrary to insert() which copies or movies existing object.
The element was newly inserted The set elements after 1st insertion are : 24 The element was already present The set elements after 2nd insertion are : 24 The set elements after 3rd insertion are : 24 25
- erase(num) : Erases the value mentioned in its argument. reorders the set after deletion.
- erase(iter) : Erases the value at the position pointed by the iterator mentioned in its argument.
- erase(strt_iter,end_iter) : Erases the range of elements starting from “strt_iter” to the “end_iter”.
Using erase() : erase() is used to erase the element in set mentioned in argument, either its position, its value or a range of number.
The set elements after insertion are : 5 10 15 20 25 30 35 40 45 The set elements after 1st deletion are : 5 15 20 25 30 35 40 45 The set elements after 2nd deletion are : 5 15 20 25 30 35 45 The set elements after 3rd deletion are : 5 15 20
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.