unordered_set insert() in C++

insert(): This function is available in the File: unordered_set.h. This function is a build-in function is STL of C++.

Approach 1: When the single value into the unordered_set.

Syntax:

std::pair<std::__detail::_Node_iterator<int, true, false>, bool> std::unordered_set<int>::insert(const int &__x) 

This function takes one argument of type as similar to the type of unordered_set as its parameter. This function attempts to insert an element into the unordered_set.

Parameters: One parameter is required for this function.

__x – Element to be inserted.

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 insert an element into the unordered_set.

Note: Insertion requires amortized constant time.

File: unordered_set.h

C++

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

int main()
{
    unordered_set<intst = {345};

    vector<intarr = {283545411};
    int value = 12;
    st.insert(value);
    cout << "Set elements are: ";
    for (auto it = st.begin(); it != st.end(); it++)
        cout << *it << " ";
    return 0;
}

Output:

Set elements are: 12 5 4 3 


Approach 2: When inserting the range elements into the unordered_set.

Syntax:

void std::unordered_set<int>::insert<std::vector<int>::iterator> (std::vector<int>::iterator __first, std::vector<int>::iterator __last) 

This function takes two arguments of type iterator. This function is a  template function that attempts to insert a range of elements.

Parameters:  Two parameters are required for this function.

__first – Iterator pointing to the start of the range to be inserted.

__last – Iterator pointing to the end of the range.

C++

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

int main()
{
    unordered_set<intst = {345};

    vector<intarr = {283545411};
    st.insert(arr.begin(), arr.end());
    cout << "Set elements are: ";
    for (auto it = st.begin(); it != st.end(); it++)
        cout << *it << " ";
    return 0;
}

Output:

Set elements are: 11 54 35 28 3 4 5


Approach 3: When inserting the initializer list into the unordered_set.

Syntax:

void std::unordered_set<int>::insert(std::initializer_list<int> __l)

This function takes one argument of type initializer_list as its parameter. This functions attempts to insert a list of elements into the unordered_set.

Parameters:  One parameter is required for this function.

__l – A std::initializer_list<value_type> of elements to be inserted.

Note: Complexity similar to that of the range constructor.

C++

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

int main()
{
    unordered_set<intst = {345};

    vector<intarr = {283545411};
    st.insert({234345});
    cout << "Set elements are: ";
    for (auto it = st.begin(); it != st.end(); it++)
        cout << *it << " ";
    return 0;
}

Output:

Set elements are: 234 3 345 4 5


Complexities:

Single element insertions

Average case: constant.

Worst case: linear in container size.

Multiple elements insertion

Average case: linear in the number of elements inserted.

Worst case: N*(size+1): number of elements inserted times the container size plus one.


No comments:

Post a Comment