Open In App

Different Ways to Initialize an Set in C++

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A set is an associative container available in the C++ Standard Template Library(STL) that is used for unique elements in a specific order, it internally uses the working principle of a Binary Search Tree to store elements.

Different Ways to Initialize a set in C++

  1. Initialization using the default constructor.
  2. Initialization using initializer list.
  3. Initialization using an array.
  4. Initialization using a vector.
  5. Initialization from another set using the copy constructor.
  6. Initialization from another iterable data structure using range constructor.

1. Initialization Using the Default Constructor

One standard way to initialize a set is to initialize using the default constructor, this will generate an empty set. Elements can be added to it using an inbuilt set.insert() method.

Syntax:

set<int>New_set;
New_set.insert(element1)

Here, the insert() method can be further used to insert elements into the set.

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <iostream>
#include <set>
using namespace std;
 
// Driver code
int main()
{   
    // Initialize set using
    // default constructor
    set<int>New_set;
   
    // set.insert() method to
    // insert elements to the set
    New_set.insert(5);
    New_set.insert(1);
    New_set.insert(3);
    New_set.insert(2);
    New_set.insert(7);
     
    // Traverse through the set(elements
    // will be returned in ascending order)
    for(auto x: New_set)
    {
       cout << x << endl;
    }
    return 0;
}


Output

1
2
3
5
7

Time Complexity : O(N logN)

Auxiliary Space : O(N)

2. Initialization using an Initializer List

Another way of initialization is to pass a predefined list of elements (initializer_list) as an argument to the default constructor of the set as shown below:

Syntax:

set<int>New_set({element1, element2, element3, element4});

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <iostream>
#include <set>
using namespace std;
 
// Driver code
int main()
{  
   // Initialize set passing initializer
   // list as an argument to the
   // default constructor
   set<int>New_set({4, 3, 9, 2, 0, 6});
    
   // Traverse through the unordered_set
   for(auto x: New_set)
   {
      cout << x << endl;
   }
   return 0;
}


Output

0
2
3
4
6
9

3. Initialization Using an Array

The elements can be added to the set using an array of the same data type.

Syntax:

set<int>New_set(old_arr, old_arr + n); 

Here, old_arr is the array of integers from which contents will be copied into the New_set.

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    // Initialize an array of
    // integers
    int old_arr[] = {4, 3, 6, 1, 8};
    int n = (sizeof(old_arr) /
             sizeof(old_arr[0]));
   
    // Adding these elements stored
    // in the array
    set<int>New_set(old_arr,
                    old_arr + n);
   
    // Traverse through the set
    for(auto x: New_set)
    {
       cout << x << endl;
    }
    return 0;
}


Output

1
3
4
6
8

4. Initialization Using a Vector

One can store the elements to the set using a vector of the same data type.

Syntax:

set<int>New_set(old_vector.begin(),old_vector.end()); 

Here, old_vector is the vector of integers from which contents will be copied into the New_set.

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    // Initialize a vector of integers
    vector<int>old_arr = {4, 1, 8, 2, 9};
   
    // Adding these elements
    // stored in the vector
    set<int>New_set(old_arr.begin(),
                    old_arr.end());
   
    // Traverse through the set
    for(auto x: New_set)
    {
       cout << x << endl;
    }
    return 0;
}


Output

Table
Cement
Floor
Grass
Ground

5. Initialization From Another Set Using the Copy Constructor

One way to initialize a set is to copy contents from another set one after another by using the copy constructor.

Syntax:

set<int>New_set(old_set);

Here, old_set is the set from which contents will be copied into the New_set

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <iostream>
#include <set>
using namespace std;
 
// Driver code
int main()
{
    // Initialize an unordered_set
    // using default constructor
    set<int>old_set;
   
    // set.insert() method to insert
    // elements to the unordered_set
    old_set.insert(5);
    old_set.insert(3);
    old_set.insert(4);
    old_set.insert(1);
    old_set.insert(2);
   
   
    // Create a new_set where contents
    // of the previous set will be
    // copied using copy constructor   
    set<int>New_set(old_set);
   
    // Traverse through the set
    for(auto x: New_set)
    {
       cout << x <<endl;
    }
    return 0;
}


Output

1
2
3
4
5

6. Initialization From Another Iterable Data Structure Using Range Constructor

Another way to initialize a set is to use a range constructor to copy elements from an iterable data structure to the newly initialized set.

Syntax:

set<int>New_set(begin(old_set), end(old_set));

Here, old_set is the set from which contents will be copied into the New_set.

Below is the C++ program to implement the above approach:

C++




// C++ program to implement
// the above approach
#include <iostream>
#include <set>
using namespace std;
 
// Driver code
int main()
{
    // Initialize a set using
    // default constructor
    set<int>old_set;
   
    // unordered_set.insert() method
    // to insert elements to the
    // unordered_set
    old_set.insert(3);
    old_set.insert(1);
    old_set.insert(4);
    old_set.insert(8);
    old_set.insert(5);
   
   
    // Create a new_set where contents
    // of the previous set will be
    // copied using range constructor   
    set<int>New_set(begin(old_set),
                    end(old_set));
   
    // Traverse through set
    for(auto x: New_set)
    {
       cout << x <<endl;
    }
    return 0;
}


Output

1
3
4
5
8


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads