The set::insert is a built-in function in C++ STL which insert elements in the set container or inserts the elements from a position to another position in the set to a different set.
- Syntax:
iterator set_name.insert(element)
Parameters: The function accepts a mandatory parameter element which is to be inserted in the set container.
Return Value: The function returns an iterator pointing to the inserted element in the container.
Below program illustrates the above function:
// CPP program to demonstrate the// set::insert(element) function#include <bits/stdc++.h>usingnamespacestd;intmain(){set<int> s;// Function to insert elements// in the set containers.insert(1);s.insert(4);s.insert(2);s.insert(5);s.insert(3);cout <<"The elements in set are: ";for(autoit = s.begin(); it != s.end(); it++)cout << *it <<" ";return0;}chevron_rightfilter_noneOutput:
The elements in set are: 1 2 3 4 5
- Syntax:
iterator set_name.insert(iterator position, element)
Parameters: The function accepts two parameter which are described below:
- element: It specifies the element to be inserted in the set container.
- position: It does not specify the position where the insertion is to be done, it only points to a position from where the searching operation is to be started for insertion to make the process faster. The insertion is done according to the order which is followed by the set container.
Return Value: The function returns an iterator pointing to the inserted element in the container.
Below program illustrates the above function:
// CPP program to demonstrate the// set::insert(iterator, element) function#include <bits/stdc++.h>usingnamespacestd;intmain(){set<int> s;// Function to insert elements// in the set containerautoitr = s.insert(s.begin(), 1);// the time taken to insertion// is very less as the correct// position for isnertion is givenitr = s.insert(itr, 4);itr = s.insert(itr, 2);itr = s.insert(itr, 5);itr = s.insert(itr, 3);cout <<"The elements in set are: ";for(autoit = s.begin(); it != s.end(); it++)cout << *it <<" ";return0;}chevron_rightfilter_noneOutput:The elements in set are: 1 2 3 4 5
- Syntax:
iterator set_name.insert(iterator position1, iterator position2)
Parameters: The function accepts two parameters position1 and position2 which specifies the range of elements. All the elements in the range [position1, last) are inserted in another set container.
Return Value: The function returns a set which has all the elements in range [position1, last).
Below program illustrates the above function:
// CPP program to demonstrate the// set::insert(iterator1, iterator2) function#include <bits/stdc++.h>usingnamespacestd;intmain(){set<int> s1;// Function to insert elements// in the set containers1.insert(1);s1.insert(4);s1.insert(2);s1.insert(5);s1.insert(3);cout <<"The elements in set1 are: ";for(autoit = s1.begin(); it != s1.end(); it++)cout << *it <<" ";set<int> s2;// Function to insert one set to another// all elements from where 3 is to end is// inserted to set2s2.insert(s1.find(3), s1.end());cout <<"\nThe elements in set2 are: ";for(autoit = s2.begin(); it != s2.end(); it++)cout << *it <<" ";return0;}chevron_rightfilter_noneOutput:The elements in set1 are: 1 2 3 4 5 The elements in set2 are: 3 4 5
Recommended Posts:
- deque insert() function in C++ STL
- vector insert() function in C++ STL
- unordered_set insert() function in C++ STL
- multiset insert() function in C++ STL
- unordered_multiset insert() function in C++ STL
- map insert() in C++ STL
- std::string::insert() in C++
- multimap insert() in C++ STL
- list insert() in C++ STL
- emplace vs insert in C++ STL
- How to insert elements in C++ STL List ?
- Inserting elements in std::map (insert, emplace and operator [])
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Function Overloading vs Function Overriding in C++
- What happens when a virtual function is called inside a non-virtual function in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



