Javatpoint Logo
Javatpoint Logo

C++ multimap emplace() function

The C++ multimap emplace() function is used to extend the multimap container by inserting new elements into the container. Elements are built directly (neither copied nor moved).

The constructor of the element is called by giving the arguments args passed to this function.

Syntax

Parameter

args: The arguments forwarded to construct an element to be inserted into the multimap.

Return value

The C++ emplace() function indicates if the insertion is occurred or not and returns an iterator pointing to the newly inserted element.

Complexity

Logarithmic in the container size.

Iterator validity

No changes.

Data races

The container is modified.

Iterating ranges in the container is not safe although concurrently accessing exiting elements is safe.

Exception Safety

If an exception is thrown, there are no changes in the container.

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 = 4
c = 3
c = 5

In the above example, it simply insert the element into the multimap m with the given key value pairs.

Example 2

Let's see a simple example to insert the element and check whether the duplicate key is allowed in multimap or not:

Output:

multimap modified, now contains 3 elements: 
(Amita,Accounting) (Deep,Engineering) (Nikita,Accounting) 

multimap modified, now contains 4 elements: 
(Amita,Accounting) (Deep,Engineering) (Nikita,Accounting) (Nikita,Engineering)

In the above example, elements are inserted in the multimap and when you try to add the same key Nikita then it will allow you to insert duplicates.

Example 3

Let's see a simple example to insert the elements into the multimap by passing a constructor argument to a key and a value respectively:

Output:

a => a
a => aaa
b => abcd
c => cccccccccc

In the above example, elements are inserted into the multimap by passing a constructor argument to a key and a value respectively.

Example 4

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

Output:

Enter the number of fmly members : 3
Enter the name and age of each member: 
Ram 42
Sita 37
Laxman 40

Total memnber of fmly is:3
Details of fmly members: 

Name    |  Age 
__________________________
Laxman | 40 
Ram      | 42 
Sita       | 37

In the above example, it simply inserts the elements by the user's choice.

Next TopicC++ multimap




Please Share

facebook twitter google plus pinterest