Open In App

set::insert() function in C++ STL

Last Updated : 04 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The std::set::insert() is a built-in function of C++ STL set container which is used to insert new elements in it. In this article, we will learn how to use set::insert() function in our C++ programs.

Syntax

The string::replace() function provides 6 different overloads for different purposes:

st.insert(val);
st.insert(pos, val);
st.insert({ val1,val2,....});
st.insert(first, last);

Following are the different ways in which we can insert elements in an std::set container using set::insert() method:

Insert a Single Element

We can use the set::insert() to insert a single in the set. It will be inserted at the position according to the order of the set elements.

Syntax

st.insert(val)

Parameters

  • val: Value which we have to insert.

Return Value

  • The version of the function returns a pair, where pair::first is an iterator pointing to either the newly inserted element or to the element with an equivalent value in the set.
    The pair::second element is a boolean value that tells whether the element was successfully inserted or not.

Note: As set contains only unique values, set::insert() function won't insert the elements that is already present in the set.

Example

C++
// C++ program to illustrate how to insert a
// single element using set::insert()
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> st;

    // Inserting an element
    st.insert(23);
  
  	// Inserting more elements
    st.insert(67);
    st.insert(2);
    st.insert(11);

    for (auto i : st)
        cout << i << " ";
    return 0;
}

Output
2 11 23 67 

Time Complexity: O(log n), where n is the number of elements already present in the set.
Auxiliary Space: O(1)

Insert an Element Near the Given Position

We can use the set::insert() function to insert the element near the given position. The std::set has to maintain the order of elements so we cannot force the insertion at any particular index.

Syntax

st.insert(pos, val)

Parameters

  • val: Value which we have to insert.
  • pos: Iterator specify the position nearby which the new element is to be inserted.

Return Value

  • This version returns an iterator pointing to the newly inserted element or the already present copy of the element.

Example

C++
// C++ Program to illustrate how to use
// set::insert() to insert the value near
// the specific position
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> st = {34, 67, 12};
  
    auto it = st.begin();

    // Inserting an element 1 at
    // starting position
    st.insert(it, 1);

    for (auto i : st)
        cout << i << " ";
    return 0;
}

Output
1 12 34 67 

Time Complexity: O(log n)
Auxiliary Space: O(1)

Insert Multiple Elements

We can insert multiple elements in a set using set::insert() by enclosing the multiple value inside the braces {} and separate each of them using a comma. This form is called initializer list.

Syntax

st.insert({ val1,val2,....});

Parameters

  • { val1,val2,....}: First value, second value and so on.

Return Value

  • This version does not return anything.

Example

C++
// C++ Program to illustrate, how to insert
// multiple values using set::insert()
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> st;

    // Insting the multiple values
    st.insert({12, 45, 11, 78, 9});
  
    for (auto i : st)
        cout << i << " ";
    return 0;
}

Output
9 11 12 45 78 

Time Complexity: O(k log n), where n is the number of elements already present in the set.
Auxiliary Space: O(k), where k is the number of elements to be inserted.

Insert Multiple Elements from a Range

The set::insert() function can also be used to insert multiple elements from the given range. This range can by any STL container.

Syntax

st.insert(first, last);

Parameters

  • first: Iterator pointing to the starting position of the range.
  • end: Iterator pointing to the position just after the last element of the range.

Return Value

  • The version of the function does not return anything.

Example

C++
// C++ program to illustrate how to  use
// set::insert to insert multiple element from
// the given range of STL container
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> vec = {12, 45, 11, 67, 9};

    // Define the range as whole vector
    auto first = vec.begin();
    auto last = vec.end();

    // Insert elements of defined range
    set<int> st(first, last);

    for (auto i : st)
        cout << i << " ";
    return 0;
}

Output
9 11 12 45 67 

Time Complexity: O(k log n), where n is the number of elements in the set.
Auxiliary Space: O(k), where k is the number of elements in the range.



Next Article
Article Tags :
Practice Tags :

Similar Reads