Javatpoint Logo
Javatpoint Logo

C++ multimap insert() function

The C++ multimap insert() is used to insert an element or range of elements into a multimap.

Syntax

Parameter

val: Key value to insert in the multimap.

position: Hint for the position to insert element.

first: Beginning of range to insert.

last: End of range to insert.

il: An initializer list.

Return value

If a single element is inserted then it returns an iterator to the position where the new element was inserted into the multimap.

Or if the element is inserted with hint then it returns an iterator that point to the position where the new element was inserted into the multimap.

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.

Exception safety

This function does not throw exception.

Example 1

Let's see the simple example to insert the elements into the multimap:

Output:

Multimap contains following elements
a = 1
b = 2
b = 3
c = 4
c = 5

In the above example, it simply insert the element with the given key value pair.

Example 2

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

Output:

Multimap contains following elements
a = 1
b = 2
c = 3
d = 4
d = 5

In the above example, elements are inserted in the defined position i.e. in begin element {'a', 1} is inserted and in the end element {'d', 5} is inserted.

Example 3

Let's see a simple example to insert the elements of one multimap to another:

Output:

Multimap contains following elements
a = 1
a = 3
b = 2
b = 5
d = 4

In the above example, multimap m1 has five elements and multimap m2 is empty. insert() is using to insert the element of m1 to m2 from begin of m1 to end of m1 and displaying the content of m2 multimap.

Example 4

Let's see a simple example to insert the element:

Output:

Multimap contains following elements
1 : Java
2 : C++
3 : SQL
3 : Oracle
4 : VB

In the above example, another form of insert() function is used to insert the elements into the multimap.

Next TopicC++ multimap




Please Share

facebook twitter google plus pinterest