Last Updated : 11 Jul, 2025
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.
SyntaxThe 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 ElementWe 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.
Syntaxst.insert(val)
Parameters
Return Value
Example C++Note: As set contains only unique values, set::insert() function won't insert the elements that is already present in the set.
// 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;
}
Time Complexity: O(log n), where n is the number of elements already present in the set.
Auxiliary Space: O(1)
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.
Syntaxst.insert(pos, val)
Parameters
Return Value
// 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;
}
Time Complexity: O(log n)
Auxiliary Space: O(1)
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.
Syntaxst.insert({ val1,val2,....});
Parameters
Return Value
// 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;
}
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.
The set::insert() function can also be used to insert multiple elements from the given range. This range can by any STL container.
Syntaxst.insert(first, last);
Parameters
Return Value
// 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;
}
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4