Multimapemplace Hint Function In C++ STL

As we all know, the C++ programming language has many in-built functions to help us avoid writing long lines of code. One such function is the multimap find function available in the rich library of C++ programming language, the Standard Template Library(STL). It will help us return an iterator that refers to or points to the position of the key containing the data traversal in the long or small multimap we have created. The multimap emplaces find very fast than the multimap emplace. It inserts the key elements with a hint, and the lead doesn't affect the position to be entered.

Multimap in C++ Code

Example

// Here we are writing down the C++ programming language code to 
// demonstrate
// the concept of multimap find() in C++ STL(Standard Template Library)

#include <iostream>
#include <iterator>
#include <map>

using namespace std;
// The main driver code functionality starts from here!
int main()
{
    // Here, we are trying to create an initializer container 
	// which will help us with creating a multimap using C++ STL
	multimap<int, int> gquiz1;

     // these insert functions which we have written below help us
	// with creating a multimap holding the values inserted below!

	gquiz1.insert(pair<int, int>(1, 40));
	gquiz1.insert(pair<int, int>(2, 30));
	gquiz1.insert(pair<int, int>(3, 60));
	gquiz1.insert(pair<int, int>(6, 50));
	gquiz1.insert(pair<int, int>(6, 10));

	 // elements that we are going to display on the Output screen are
	multimap<int, int>::iterator itr;
	cout << "\nThe multimap gquiz1 is : \n";
	cout << "\tKEY\tELEMENT\n";
	// here we are writing the for loop, which contains the auto code
	// functionality containing the find() function at different positions
	for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
		cout << '\t' << itr->first
			<< '\t' << itr->second << '\n';
	}
	cout << endl;
	
	// here, we are trying to add elements randomly,
	// as it helps us to check the sorted keys property in C++ STL
	gquiz1.insert(pair<int, int>(4, 50));
	gquiz1.insert(pair<int, int>(5, 10));

	// the below code helps us with printing multimap gquiz1 again

	cout << "\nmultimap quiz1 after"
		<< " adding the extra elements is: \n";
	cout << "\tkey\telement\n";
	for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
		cout << '\t' << itr->first
			<< '\t' << itr->second << '\n';
	}
	cout << endl;

	// the below code snippet below assigning the elements from quiz1 to quiz2
	multimap<int, int> gquiz2(gquiz1.begin(),
							gquiz1.end());

// the below code snippet prints all the elements of the multimap gquiz2
	cout << "\nThe multimap gquiz2 after"
		<< " assign from gquiz1 is : \n";
	cout << "\tKEY\tELEMENT\n";
	for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
		cout << '\t' << itr->first
			<< '\t' << itr->second << '\n';
	}
	cout << endl;

	
return 0;
    
}

Output:

Output

The multimap gquiz1 is : 
	KEY	ELEMENT
	1	40
	2	30
	3	60
	6	50
	6	10

Multimap quiz1 after adding the extra elements is : 
	key	element
	1	40
	2	30
	3	60
	4	50
	5	10
	6	50
	6	10

The multimap gquiz2 after assign from gquiz1 is : 
	KEY	ELEMENT
	1	40
	2	30
	3	60
	4	50
	5	10
	6	50
	6	10

multimap::emplace_hint Function in C++ STL

Syntax

Example

multimap_name.emplace_hint(position, key, element)

C++ Code

Example

// Here we are writing down the C++ programming language code to 
// demonstrate
// the concept of multimap::emplace_hint() function in the 
// C++ STL(Standard Template Library)
#include <bits/stdc++.h>
using namespace std;
// the main driver code functionality starts from here 
int main()
{

    // Here, we are trying to create an initializer container 
	// which will help us with creating a multimap using C++ STL
	multimap<int, int> mp;

     // these emplace functions which we have written below help us
	// with creating a multimap holding the values inserted below!

	mp.emplace_hint(mp.begin(), 12, 230); // this is faster
	mp.emplace_hint(mp.begin(), 14, 440); // this is faster
	mp.emplace_hint(mp.begin(), 42, 960); // this is slower
	mp.emplace_hint(mp.begin(), 32, 620); // this is slower
	mp.emplace_hint(mp.begin(), 31, 540); // this is faster
	mp.emplace_hint(mp.begin(), 17, 150); // this is faster

    // elements that we are going to display on the Output screen are
	cout << "\nThe multimap is : \n";
	cout << "KEY\tELEMENT\n";
	
	// here we are writing the for loop, which contains the auto code
	// functionality containing the find() function at different positions 
	for (auto itr = mp.begin(); itr != mp.end(); itr++)
		cout << itr->first << "\t" << itr->second << endl;

	return 0;
	// The main driver code functionality ends from here!
}

Output:

Output

The multimap is : 
KEY	ELEMENT
12	230
14	440
17	150
31	540
32	620
42	960

Input Required

This code uses input(). Please provide values below: