Set insert() in C++ STL


In this article we are going to discuss the set::insert() function in C++ STL, their syntax, working and their return values.

What is Set in C++ STL?

Sets in C++ STL are the containers which must have unique elements in a general order. Sets must have unique elements because the value of the element identifies the element. Once added a value in set container later can’t be modified, although we can still remove or add the values to the set. Sets are used as binary search trees.

What is set::insert()?

insert() function is an inbuilt function in C++ STL, which is defined in <set> header file. This function is used to insert elements in the set container. when we insert the element the size of the container is increased by the number of the elements inserted. As the set contains the unique values, insert() not only inserts the element, it first checks whether the element which is to be inserted is not present in the set container. Also, in set all the elements are stored in the sorted position, so the element we will insert will be inserted according to its sorted position.

Syntax

Set1.insert(const type_t &value); ----(1)
Or
Set1.insert(iterator position, const type_t &value); -----(2)
Or
Set1.insert(iterator position_1, iterator position_2); -----(3)

Parameters

  • value − It is the value which is to be inserted in the set container.

  • position − It is the hint to the position, it will start searching from this position and inserts the element where it is suited to be inserted.

  • position_1, position_2 − These are the iterator which specifies the range which is to be inserted in the set associated with insert(). position_1 for starting of the range and position_2 for the end of the range.

Return value

The function returns different types of values according to the arguments passed in the function.

  • When we pass only the value; the function returns the iterator pointing to the element which is being inserted in the set container.

  • When we pass position with value; the function again returns the iterator pointing to the element which is being inserted in the set container.

  • When we pass the position_1 and position_2; the function returns the set of the values which comes between the range starting from the position_1 and ending at position_2.

Examples

Input: set<int> myset;
   myset.insert(10);
Output: values in the set = 10
Input: set <int> myset = {11, 12, 13, 14};
   myset.insert(myset.begin(), 10);
Output: values in the set = 10 11 12 13 14

Example

Inserting elements in a set in a queue i.e. one after the another

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   set<int> mySet;
   mySet.insert(10);
   mySet.insert(20);
   mySet.insert(30);
   mySet.insert(40);
   mySet.insert(50);
   cout<<"Elements are: ";
   for (auto i = mySet.begin(); i != mySet.end(); i++)
      cout << *i << " ";
   return 0;
}

Output

If we run the above code then it will generate the following output −

Elements are : 10 20 30 40 50

Example

Inserting elements to the set based upon the position

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   set<int> mySet;
   auto i = mySet.insert(mySet.begin(), 10);
   i = mySet.insert(i, 20);
   i = mySet.insert(i, 40);
   i = mySet.insert(i, 30);
   i = mySet.insert(i, 80);
   i = mySet.insert(mySet.end(), 90);
   cout<<"Elements are: ";
   for (auto i = mySet.begin(); i != mySet.end(); i++)
      cout << *i << " ";
   return 0;
}

Output

If we run the above code then it will generate the following output −

Elements are: 10 20 30 40 80 90

Updated on: 05-Mar-2020

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements