Multiset emplace_hint() function in C++ STL



In this article we will be discussing the working, syntax and examples of multiset::emplace_hint() function in C++ STL.

What is a multiset in C++ STL?

Multisets are the containers similar to the set container, meaning they store the values in the form of keys same like a set, in a specific order.

In multiset the values are identified as keys as same as sets. The main difference between multiset and set is that the set has distinct keys, meaning no two keys are the same, in multiset there can be the same keys value.

Multiset keys are used to implement binary search trees.

What is multiset::emplace_hint()?

multiset::emplace_hint() function is an inbuilt function in C++ STL, which is defined in <set> header file. This function is used to insert a new element with a hint in the multiset container associated.

The hint is taken to tell the function where we want to emplace the new element and then it constructs and inserts the element at the corresponding position. This function increases the size of the container by 1.

Syntax

ms_name.emplace_hint(const_iterator position, args&& val);

Parameters

The function accepts the following Parameters −

  • position − The hint position where we want the element to be emplaced.
  • val − The values which we want to be constructed and emplaced after the specified position.

Return value

This function returns the iterator pointing to the element emplaced.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   multiset<int> check;
   auto i = check.emplace_hint(check.begin(), 4);
   i = check.emplace_hint(i, 1);
   check.emplace_hint(i, 9);
   check.emplace_hint(i, 10);
   cout<<"Elements are : ";
   for (auto i = check.begin(); i!= check.end(); i++)
      cout << *i << " ";
   return 0;
}

Output

If we run the above code it will generate the following output −

Elements are : 1 4 9 10

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   multiset<int> check;
   auto i = check.emplace_hint(check.begin(), 45);
   i = check.emplace_hint(i, 40);
   check.emplace_hint(i, 42);
   check.emplace_hint(i, 30);
   check.emplace_hint(check.begin(), 61);
   check.emplace_hint(check.begin(), 6);
   check.emplace_hint(check.begin(), 36);
   cout<<"List is : ";
   for (auto i = check.begin(); i != check.end(); i++)
      cout << *i << " ";
   return 0;
}

Output

If we run the above code it will generate the following output −

Elements are : 6 30 36 40 42 45 61
raja
Published on 23-Mar-2020 07:51:44
Advertisements