Different ways to Insert Elements in Set

Sets are a type of associative container in which each element must be distinct because its value identifies it. The element’s value cannot be changed after it is added to the set, but it is possible to remove and re-add the element’s modified value.

Inserting elements in an 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.

2)Insert single values to set

Insert function accepts the element to be inserted and returns an Iterator and a bool flag pair.
If the insertion is successful, the value of the bool flag in the returned pair is true, and the iterator in it points to the newly inserted element.
If the insertion fails due to a duplicate element, the bool flag in the returned pair will be false.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
void insertElement(set<std::string>& stringset,
                   string given_string)
{
    // Taking a variable res which is of type pair
    pair<set<string>::iterator, bool> res;
    // insert function which returns iterator and boolean
    // value
    res = stringset.insert(given_string);
    // Checking if the given string is inserted succesfully
    // or not
    if (res.second)
        cout << given_string << "  inserted to set" << endl;
    else
        cout << given_string << " not inserted to set"
             << endl;
}
int main()
{
    // initializing a string set
    set<std::string> stringset;
    // inserting elements to set(may contain duplicate
    // elements)
    insertElement(stringset, "Hello");

    insertElement(stringset, "this");
    insertElement(stringset, "Hello");
    insertElement(stringset, "this");
    insertElement(stringset, "BTechGeeks");
    cout << "---------Elements of set-----------" << endl;
    // printing the elements of set
    for (auto value : stringset)
        cout << value << endl;
    return 0;
}

Output:

Hello  inserted to set
this  inserted to set
Hello not inserted to set
this not inserted to set
BTechGeeks  inserted to set
---------Elements of set-----------
BTechGeeks
Hello
this

3)Inserting elements from vector to set

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

void insert ( InputIterator first, InputIterator last );

We can use this to insert elements into theĀ  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  set
    set<int> newset;
    // initializing  set with given vector
    newset.insert(vect.begin(), vect.end());
    // printing the set
    for (int value : newset)
        std::cout << value << " ";
    return 0;
}

Output:

1 2 3 4 5

4)Inserting elements from initializer list to set

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 set
    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)
        cout << value << " ";
    return 0;
}

Output:

1 2 3 4 5 6 7 9 12

Related Programs: