The emplace_hint method in C++ Multiset is employed to expand the multiset container by adding fresh elements into the container with a hint specifying the position for the element. The elements are constructed directly, without being copied or moved.
The element's constructor is invoked by providing the arguments args that are passed to this method.
Syntax
template <class... Args>
iterator emplace_hint (const_iterator position, Args&&... args); //since C++ 11
Parameter
The parameters passed to build an element for insertion into the container.
The ```
include <iostream>
include <set>
using namespace std;
int main
{
multiset<char> mymultiset;
auto it = mymultiset.end;
it = mymultiset.emplace_hint(it,'b');
mymultiset.emplace_hint(it,'a');
mymultiset.emplace_hint(mymultiset.end,'b');
cout << "mymultiset contains:";
for (auto& x: mymultiset)
cout << " [" << x << ']';
cout << '\n';
return 0;
}
## Return value
The function emplace_hint() provides an iterator pointing to the recently added elements. In case the element already exists, the insertion process fails, and it returns an iterator pointing to the existing element.
## Complexity
If a specific position is not indicated, the complexity will increase logarithmically as the size of the container grows.
If a position is specified, the complexity will be constant on average over time.
## Iterator validity
No changes.
## Data Races
The container is modified.
Iterating through ranges within the container poses potential risks, whereas accessing existing elements concurrently is considered safe.
## Exception Safety
If an error is raised, the container remains unchanged.
## Example 1
Let's examine a straightforward example of adding elements to a multiset:
include <iostream>
include <set>
using namespace std;
int main(void) {
multiset<int> m = {30, 20, 30, 10};
m.emplace_hint(m.end, 40);
m.emplace_hint(m.begin, 20);
cout << "Multiset contains following elements" << endl;
for (auto it = m.begin; it != m.end; ++it)
cout << *it<< endl;
return 0;
}
Output:
Multiset contains following elements
10
20
20
30
30
40
In the previous example, it effectively adds the element to the multiset m with the specified value at the designated positions.
## Example 2
Let's see a simple example:
include <set>
include <string>
include <iostream>
using namespace std;
template <typename M> void print(const M& m) {
cout << m.size << " elements: " << endl;
for (const auto& p : m) {
cout << p << " " ;
}
cout << endl;
}
int main
{
multiset<string> m1;
// Emplace some test data
m1.emplace("Ram");
m1.emplace("Deep");
m1.emplace("Sunil");
cout << "multiset starting data: ";
print(m1);
cout << endl;
// Emplace with hint
// m1.end should be the "next" element after this emplacement
m1.emplace_hint(m1.end, "Deep");
cout << "multiset modified, now contains ";
print(m1);
cout << endl;
return 0;
}
Output:
multiset starting data: 3 elements:
Deep Ram Sunil
multiset modified, now contains 4 elements:
Deep Deep Ram Sunil
## Example 3
Let's consider a basic example demonstrating how to add elements to a multiset at specified positions:
include <iostream>
include <set>
using namespace std;
int main
{
multiset<char> mymultiset;
auto it = mymultiset.end;
it = mymultiset.emplace_hint(it,'b');
mymultiset.emplace_hint(it,'a');
mymultiset.emplace_hint(mymultiset.end,'b');
cout << "mymultiset contains:";
for (auto& x: mymultiset)
cout << " [" << x << ']';
cout << '\n';
return 0;
}
Output:
mymultiset contains: [a] [b] [b]
## Example 4
Let's see a simple example to insert the element:
include <iostream>
include <set>
include <string>
using namespace std;
int main {
typedef multiset<string> city;
string name;
city fmly ;
int n;
cout<<"Enter the number of family members :";
cin>>n;
cout<<"Enter the name of each member: \n";
for(int i =0; i<n; i++)
{
cin>> name; // Get key
fmly.emplace_hint(fmly.begin,name);
}
cout<<"\nTotal members of family is:"<< fmly.size;
cout<<"\nDetails of family members: \n";
cout<<"\nName \n ________________________\n";
city::iterator p;
for(p = fmly.begin; p!=fmly.end; p++)
{
cout<<(*p) <<" \n ";
}
return 0;
}
Output:
Enter the number of fmly members : 4
Enter the name of each member:
Deep
Sonu
Ajeet
Bob
Total memnber of fmly is:4
Details of fmly members:
Name
________________________
Ajeet
Bob
Deep
Sonu
In the example provided, it adds the selected elements to the start of a multiset as chosen by the user.