Javatpoint Logo
Javatpoint Logo

C++ map emplace_hint() function

C++ map emplace_hint() function is used to extend the map container by inserting new elements into the container using hint as a position for element. Elements are built directly (neither copied nor moved).

The constructor of the element is called by giving the arguments args passed to this function. Insertion takes place only if the key is not present already.

Syntax

Parameter

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

position: Hint for the position to insert the new elements.

Return value

It returns an iterator to the newly inserted elements. If the element already exists, insertion is failed and returns an iterator to the existing element.

Example 1

Let's see a simple example to insert the elements into the map.

Output:

Map contains following elements
a = 10
b = 20
c = 30
d = 40
e = 50

In the above example, it simply inserts the element into the map m with the given key value pairs and positions.

Example 2

Let's see a simple example.

Output:

  map starting data: 3 elements: 
(Rakesh,Accounting) (Ram,Accounting) (Sunil,Engineering) 

map modified, now contains 4 elements: 
(Deep,Engineering) (Rakesh,Accounting) (Ram,Accounting) (Sunil,Engineering)

Example 3

Let's see a simple example to insert the elements into the map with the given position.

Output:

mymap contains: [a:12] [b:10] [c:14]

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 in the beginning of the map.

Next TopicC++ Map



Please Share

facebook twitter google plus pinterest