C++ set emplace_hint
The C++ set emplace_hint method is employed to expand the set data structure by adding fresh elements into it with a hint specifying the position for the element. The elements are constructed in place, without being copied or moved.
The constructor of the element is invoked by providing the arguments args that are passed to this function.
Insertion occurs exclusively when the key is not already present.
Syntax
template <class.... Args>
iterator emplace_hint (const_iterator position, Args&&... args); //since C++ 11
Parameter
The parameters passed to create an element to be added to the set.
The ```
include <iostream>
include <set>
using namespace std;
int main
{
set<char> myset;
auto it = myset.end;
it = myset.emplace_hint(it,'b');
myset.emplace_hint(it,'a');
myset.emplace_hint(myset.end,'c');
cout << "myset contains:";
for (auto& x: myset)
cout << " [" << x << ']';
cout << '\n';
return 0;
}
## Return value
It provides an iterator to the recently added elements. If the element already exists, the insertion fails and returns an iterator to the existing element.
## Complexity
If no position is specified, the complexity will be logarithmic in the size of the container.
If a position is provided, the complexity will be constant on average over time.
## Iterator validity
No changes.
## Data Races
The container is modified.
Iterating through containers of ranges is considered unsafe, while concurrently accessing existing elements is considered safe.
## Exception Safety
If an error is raised, the container remains unchanged.
## Example 1
Let's explore a straightforward example of adding elements to a set:
include <iostream>
include <set>
using namespace std;
int main(void) {
set<int> m = {60, 20, 30, 40};
m.emplace_hint(m.end, 50);
m.emplace_hint(m.begin, 10);
cout << "Set contains following elements" << endl;
for (auto it = m.begin; it != m.end; ++it)
cout << *it<< endl;
return 0;
}
Output:
Set contains following elements
10
20
30
40
50
60
In the example provided, it essentially adds the element to the set 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
{
set<string> m1;
// Emplace some test data
m1.emplace("Ram");
m1.emplace("Rakesh");
m1.emplace("Sunil");
cout << "set 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 << "set modified, now contains ";
print(m1);
cout << endl;
}
Output:
set starting data: 3 elements:
Rakesh Ram Sunil
set modified, now contains 4 elements:
Deep Rakesh Ram Sunil
## Example 3
Let's examine a basic example of adding elements to a set at specified positions:
include <iostream>
include <set>
using namespace std;
int main
{
set<char> myset;
auto it = myset.end;
it = myset.emplace_hint(it,'b');
myset.emplace_hint(it,'a');
myset.emplace_hint(myset.end,'c');
cout << "myset contains:";
for (auto& x: myset)
cout << " [" << x << ']';
cout << '\n';
return 0;
}
Output:
myset contains: [a] [b] [c]
## Example 4
Let's examine a basic illustration for adding the elements:
include <iostream>
include <set>
include <string>
using namespace std;
int main {
typedef set<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 memnbers in family are:"<< 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 elements chosen by the user to the start of the set.