Javatpoint Logo
Javatpoint Logo

Set insertion and deletion in C++

A Set is a container built in C++ with a concept comparable to the set described in mathematics. The set differs from other containers in that it includes only distinct elements, which may be traversed in sorted order. A good understanding of sets is important in competitive programming and overcoming algorithmic issues. In this article, we will discuss the set insertion and deletion in C++.

Set Insertion

We can insert elements into the set container by using two member methods of std::set:

  1. Making use of the insert() function
  2. Making use of the emplace() function

1. Insertion using the insert() Function

Syntax: 1

The insert() method is used to insert the set's components. After insertion, the elements are reordered, and the set is sorted. This function can be executed in three different ways.

This function adds an element to the set. The insertion occurs when the element passed is not already in the set. It gives back a pointer pair. The first element refers to elements that already exist or have been added. The second element yields "true" or "false" as the boolean result.

Syntax: 2

The indication of the pointer is given along with the element to be inserted in this implementation. A hint pointer assists insert() in determining where the insertion should occur. As a result, trying to shorten the time required to allocate the element. The hint pointer doesn't require insertion at a specified location. This function returns the place where the element was inserted as a pointer.

Syntax: 3

This insertion type must be used to add elements from other containers to the set. If the repeated components have been found in the source container, they are not added.

Example:

Let's take an example to demonstrate the set insertion using the insert function in C++:

Output:

Newly inserted element
The elements of the set after the first insertion are 30 
The elements of the set after the second insertion are: 30 34 
The elements of the set after the third insertion are: 25 26 30 34 

2. Making use of the emplace() function

The emplace() function also serves to insert the component into the Set. This function is similar to "insert()" explained above, with the only difference becoming that the element is constructed "in-place" at the exact location of element insertion, as opposed to insert(), which copies objects that already exist.

Syntax of emplace():

This method increases the total number of elements of the set by one. It returns an object with a pointer pair, the initial one of which is an iterator pointing to the current location of the inserted element and the second of which is a variable with a boolean value indicating if the element is already existent or freshly generated.

Syntax emplace_hint():

Iterates over a "hint_iterator" to get a hint of the insertion point, possibly decreasing the time necessary to insert the inserted element. It does not affect the insertion position. It occurs where it is defined locally.

Example:

Let's take an example to demonstrate the set insertion using the emplace() function in C++:

Output:

Newly inserted element
The elements of the set after the first insertion are: 25 
The element was inserted already
The elements of the set after the second insertion are: 25 
The elements of the set after the third insertion are: 25 29 

Set Deletion

Using the erase() function, we can remove elements from a set container. It is a function in the std::set class. It has the following applications:

Syntax 1:

Removes the value specified in the argument. After deletion, reorder the set.

Syntax 2:

Removes the value from the position indicated by the iterator in its parameter.

Syntax 3:

Removes the elements from "begining_iterator" to "ending_iterator".

Example:

Let's take an example to demonstrate the set deletion using the erase() function in C++:

Filename: Deletion. cpp

Output:

The elements of the set after the insertion are: 2 4 6 8 10 12 14 16 18 
The elements of the set after the first deletion are: 6 8 10 12 14 16 18 
The elements of the set after the second deletion are:2 6 8 10 12 14 16 18 
The elements of the set after the third deletion are:2 6 8 






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA