Unordered_set insert – How to Insert Elements in an Unordered_Set

Unordered Set:

Unordered_set insert: Since the value of an element defines it, sets are a form of associative container in which each element must be special. The element’s value cannot be changed after it has been added to the set, but the element’s modified value can be removed and added again.

A hash table is used to implement an unordered set, with keys hashed into hash table indices to ensure that insertion is always random. All operations on the unordered set take constant time O(1) on average, which can go up to linear time O(n) in the worst case depending on the internally used hash function, but they perform very well in practise and generally provide a constant time lookup operation.

Inserting elements in an Unordered_set

1)insert() function

The set::insert function is a built-in function in C++ STL that inserts elements into the set container or inserts elements from one position in the set to another in the set to a different set.

Syntax: 

iterator set_name.insert(element)

Parameters:

The function requires a mandatory parameter element to be inserted into the set container.

Return:

An iterator pointing to the inserted element in the container is returned by the function

Time Complexity:

log(N), where N denotes the number of elements in the set.

Method #1:Insert Values to set

We can insert values to set as shown below:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating new unordered set
    unordered_set<string> newset;
    // inserting values
    newset.insert("Hello");
    newset.insert("this");
    newset.insert("is");
    newset.insert("BTechGeeks");
    // printing the set
    for (string value : newset)
        std::cout << value << " ";
    return 0;
}

Output:

is BTechGeeks Hello this

Method #2:Inserting elements from vector to unordered_set

Another overloaded version of insert() that accepts a range is provided by std::unordered set is given below

void insert ( InputIterator first, InputIterator last );

We can use this to insert elements into the unordered set from any container or array.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{

    // creating a vector
    vector<int> vect({ 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 });
    // creating new unordered set
    unordered_set<int> newset;
    // initializing unordered set with given vector
    newset.insert(vect.begin(), vect.end());
    // printing the set
    for (int value : newset)
        std::cout << value << " ";
    return 0;
}

Output:

5 4 3 2 1

Method #3:Inserting elements from initializer list to unordered_set

unordered set has a constructor that takes an initialzer list as an argument and uses it to initialise the set.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{

    //  create unordered set
    unordered_set<int> newset;
    // using insert() function inserting elements from
    // initializer list

    newset.insert({ 1, 2, 3, 4, 5, 6, 7, 12, 3, 9, 5 });
    // printing the set
    for (int value : newset)
        std::cout << value << " ";

    return 0;
}

Output:

9 12 7 6 5 4 3 2 1

Related Programs: