Home » C++ set erase() Function

C++ set erase() Function

by Online Tutorials Library

C++ set erase()

C++ set erase() function is used to remove either a single element associated with given key or a range of elements ([first, last)) from the set container. Hence, the size will be reduced by the number of elements removed.

Syntax

Parameter

position: iterator pointing to a single element to be removed from the set.

val: Value to be removed from the set.

first: Beginning of the range to erase.

last: End of the range to erase.

Return value

It returns an iterator that points to the next element of the deleted element or returns the number of deleted elements.

Complexity

erase(position): Amortized constant.

erase (val): Logarithmic in container size.

erase(first,last): Linear in the distance between first and last.

Iterator validity

Iterators, references and the pointers pointing to elements removed by the function are invalidated.

All other iterators, pointers and references keep their validity.

Data Races

The container is modified.

The elements removed are modified. Iterating ranges in the container is not safe although concurrently accessing other elements is safe.

Exception Safety

This function does not throw exception.

If an invalid range or position is specified, it causes undefined behavior.

Example 1

Let’s see the simple example to erase the element by the iterator.

Output:

Before erasing the element:   10  20  30    After erasing the element:   10  20  30  

In the above example, element is erased by the iterator it.

Example 2

Let’s see a simple example to erase the element of the set with the given key value:

Output:

Before erasing the element:   10  20  30  40    After erasing the element:   10  20  40  

In the above example, erase(value) function uses the value 30 from the set.

Example 3

Let’s see a simple example to erase the element by the given range:

Output:

Before erasing the element are:   Size is: 3  10  20  30    After erasing the element are:   Size is: 0  

In the above example, erase(first, last) function is used to erase the element with the given range i.e. begin to end.

Example 4

Let’s see a simple example to erase all the odd numbers from the set:

Output:

After erasing odd numbers, elements are:   2, 4, 6, 8, 10, 12, 14,  

In the above example, all the odd numbers has been erased and displaying even numbers.

Example 5

Let’s see another example:

Output:

Starting data of set s1 is:   [Bert] [Bob] [Bobby] [Rob] [Robert]  size() == 5    After the 2nd element is deleted, the set s1 is:   [Bert] [Bobby] [Rob] [Robert]  size() == 4    Starting data of set s2 is:   [growl] [hiss] [meow] [purr] [yowl]  size() == 5    After the middle elements are deleted, the set s2 is:   [growl] [yowl]  size() == 2    Starting data of set s3 is:   [A] [A#] [B] [C] [C#] [D] [D#] [E] [E#] [F] [F#] [G] [G#]  size() == 13    The number of elements removed from s3 is: 1.  After the element with a key of "E#" is deleted, the set s3 is:   [A] [A#] [B] [C] [C#] [D] [D#] [E] [F] [F#] [G] [G#]  size() == 12  

You may also like