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++:
- Initialization using the default constructor.
- Initialization using initializer list.
- Initialization using an array.
- Initialization using a vector.
- Initialization from another set using the copy constructor.
- 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++
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< int >New_set;
New_set.insert(5);
New_set.insert(1);
New_set.insert(3);
New_set.insert(2);
New_set.insert(7);
for ( auto x: New_set)
{
cout << x << endl;
}
return 0;
}
|
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++
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< int >New_set({4, 3, 9, 2, 0, 6});
for ( auto x: New_set)
{
cout << x << endl;
}
return 0;
}
|
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int old_arr[] = {4, 3, 6, 1, 8};
int n = ( sizeof (old_arr) /
sizeof (old_arr[0]));
set< int >New_set(old_arr,
old_arr + n);
for ( auto x: New_set)
{
cout << x << endl;
}
return 0;
}
|
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int >old_arr = {4, 1, 8, 2, 9};
set< int >New_set(old_arr.begin(),
old_arr.end());
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++
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< int >old_set;
old_set.insert(5);
old_set.insert(3);
old_set.insert(4);
old_set.insert(1);
old_set.insert(2);
set< int >New_set(old_set);
for ( auto x: New_set)
{
cout << x <<endl;
}
return 0;
}
|
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++
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< int >old_set;
old_set.insert(3);
old_set.insert(1);
old_set.insert(4);
old_set.insert(8);
old_set.insert(5);
set< int >New_set(begin(old_set),
end(old_set));
for ( auto x: New_set)
{
cout << x <<endl;
}
return 0;
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Apr, 2023
Like Article
Save Article