Home » C++ multiset insert() function

C++ multiset insert() function

by Online Tutorials Library

C++ multiset insert()

C++ Multiset insert() function is used for inserting new element or a range of elements in the multiset.

Syntax

Parameter

val: Value to insert in the multiset.

position: Hint for the position to insert element in the multiset.

first: Beginning of range to insert value.

last: End of range to insert value.

il: An initializer list.

Return value

The insert() function return an iterator pointing to the newly inserted element in the multiset.

Complexity

If a single element is inserted, complexity will be logarithmic in size.

If a hint is given and the position given is the optimal, then the complexity will be amortized constant.

Iterator validity

No changes.

Data Races

The container is modified.

Concurrently accessing the existing elements of multiset is safe, although iterating ranges in the container is not.

Exception Safety

This function does not throw exception.

Example 1

Let’s see the simple example to insert the elements into the multiset:

Output:

The elements in multiset are: 1 2 3 3 4  

In the above example, it simply inserts the element with the given key.

Example 2

Let’s see a simple example to insert the element in the specified position:

Output:

The elements in multiset are: 1 2 3 4 4   

In the above example, elements are inserted in the defined position.

Example 3

Let’s see a simple example to insert the elements of one multiset to another in a given range:

Output:

The elements in multiset1 are: 1 2 3 4 4   The elements in multiset2 are: 3 4 4    

Example 4

Let’s see a simple example to insert the elements from the initializer list:

Output:

Multiset contains following elements  C++  C++  Java  Oracle  SQL  

In the above example, elements are inserted from an initializer list.

Next TopicC++ multiset

You may also like