C++ Standard Library C++ STL Library

C++ set - set() Function



The C++ set::set function is used to construct a set object, initializing its contents depending on the version of constructor used:

Syntax

//default version - construct an empty 
//container with no elements
explicit set (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& alloc = allocator_type());

//copy version - copies all elements
//of x into the container	
set (const set& x);
//default version - construct an empty 
//container with no elements
explicit set (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
explicit set (const allocator_type& alloc);

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());

//copy version - copies all elements
//of x into the container	
set (const set& x);
set (const set& x, const allocator_type& alloc);

//move version - moves elements of x
//into the container	
set (set&& x);
set (set&& x, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container	
set (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());
//default version - construct an empty 
//container with no elements
set();
explicit set (const key_compare& comp,
              const allocator_type& alloc = allocator_type());
explicit set (const allocator_type& alloc);

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());
template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const allocator_type& = allocator_type());

//copy version - copies all elements
//of x into the container	
set (const set& x);
set (const set& x, const allocator_type& alloc);

//move version - moves elements of x
//into the container	
set (set&& x);
set (set&& x, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container		
set (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());
set (initializer_list<value_type> il,
     const allocator_type& alloc = allocator_type());

Parameters

alloc Specify the Allocator object. The container keeps and uses an internal copy of this allocator.
comp A binary predicate that takes two elements of set as arguments and returns a bool. Elements are sorted by using this function.
first Specify initial position of the input iterator of the range. The range used is [first,last).
last Specify final position of the input iterator of the range. The range used is [first,last).
x Specify a set object of same type.
il Specify an initializer_list object.

Return Value

Constructor never returns value.

Time Complexity

Constant i.e, Θ(1), for default version and move version.
For all the other cases, Linear i.e, Θ(n).

Example: using default version

In the example below, the set::set function is used to construct a set object.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  //default version - construct an empty set
  set<int> MySet;
  set<int>::iterator it;

  cout<<"MySet contains: ";
  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<*it<<" ";

  //insert elements in MySet
  MySet.insert(10);
  MySet.insert(20);

  cout<<"\nMySet contains: ";
  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

MySet contains: 
MySet contains: 10 20 

Example: using range and copy version

A set can also be constructed using range or copy version. Consider the following example:

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  set<int> set1{100, 200, 300};
  set<int>::iterator it;

  //range version - construct set2 using range
  set<int> set2(set1.begin(), set1.end());

  //copy version - construct set3 from set1
  set<int> set3(set1); 

  cout<<"set2 contains: ";
  for(it = set2.begin(); it != set2.end(); ++it)
    cout<<*it<<" ";

  cout<<"\nset3 contains: ";
  for(it = set3.begin(); it != set3.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

set2 contains: 100 200 300
set3 contains: 100 200 300 

Example: using move version

Using the move version of set, the content of one set can be moved to another set. Consider the following example:

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  set<int> set1{10, 20, 30, 40, 50};
  set<int>::iterator it;

  cout<<"set1 contains: ";
  for(it = set1.begin(); it != set1.end(); ++it)
    cout<<*it<<" ";

  //moving all content of set1 into set2
  set<int> set2(move(set1));

  cout<<"\nset1 contains: ";
  for(it = set1.begin(); it != set1.end(); ++it)
    cout<<*it<<" ";

  cout<<"\nset2 contains: ";
  for(it = set2.begin(); it != set2.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

set1 contains: 10 20 30 40 50 
set1 contains: 
set2 contains: 10 20 30 40 50 

Example: using initializer list version

The initializer list can also be used to assign values into a set container. Consider the example below:

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  //creating initializer list
  initializer_list<int> ilist = {15, 30, 45, 60, 75};

  //initializer list version - copies all 
  //elements of ilist into the container	
  set<int> MySet(ilist);
  set<int>::iterator it;

  cout<<"MySet contains: ";
  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

MySet contains: 15 30 45 60 75 

❮ C++ <set> Library