set emplace_hint() in C++

emplace_hint(): This is a build-in function in STL.This function attempts to insert an

element into the set. This function returns an iterator that points to the element with a key equivalent to the one generated from __args (may or may not be the element itself). 

This function is not concerned about whether the insertion took place, and thus does not return a boolean like the single-argument emplace() does. 

Note: The first parameter is only a hint and can potentially improve the performance of the insertion process.

Parameters: Two parameters are required for this function.

__pos – An iterator that serves as a hint as to where the element should be inserted. 

__args – Arguments used to generate the element to be inserted. 

This function is available in the below file.

File: stl_set.h

Syntax:

st.emplace(__pos, __args)

For Example:

st = {1,2,3,4}

st.emplace(st.begin(),5) = > It insert 5 into the set.

So set becomes = {1,2,3,4,5}

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    set<intst;
    st.insert(4);
    st.insert(2);
    st.insert(3);
    st.insert(1);

    st.emplace_hint(st.begin(), 5);

    for (auto it = st.begin(); it != st.end(); it++)
        cout << *it << " ";

    return 0;
}


No comments:

Post a Comment