set emplace() in C++

emplace(): This function is a build-in function in STL. This function attempts to build and 

insert an element into the set. 

Parameters: One parameter is required for this function.

 __args – Arguments used to generate an element. 

This function returns a pair, of which the first element is an iterator that points to the possibly inserted element, and the second is a bool that is true if the element was actually inserted. This function attempts to build and insert an element into the set. A set relies on unique keys and thus an element is only inserted if it is not already present in the set. Insertion requires logarithmic time.

This function is available in the below file.

File: stl_set.h

Syntax:

st.emplace(__args)

For Example:

st = {1,2,3,4}

st.emplace(6) => It inserts 6 into the set.

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(6);

    cout << st.size() << "\n";

    return 0;
}


No comments:

Post a Comment