Sets in C++ are containers that store unique elements in a sorted manner. The elements inside the set cannot be changed, once they are added to the set, they can only be inserted or deleted. They are implemented as BST (Binary Search Trees) in memory. Set is present in #include<set> header file. The elements inside the set can be accessed using iterators.
Set Declaration
Syntax:
set <data_type> name = {initial_values};
data_type: data type of the elements to be stored inside the set
initial_values: optional parameter which initializes the set with the given values
Note: By default the set stores values in non-decreasing order. To store the values in non-increasing order, we use an inbuilt comparator function
set <data_type, greater <data_type>> name;
Example:
set <int> s; //initializes a set of size 0 which stores integer values arranged in non-decreasing order
set <int> s = {10, 20, 30}; //initializes a set having initial values as 10,20,30
set <int, greater <int>> s; //initializes a set of size 0 which stores integer values arranged in non-increasing orderFunctions on sets
begin(): Returns an iterator to the first element of the set.
Parameters: None
Return type: iterator
end(): Returns an iterator to the element past the last element of the set.
Parameters: None
Return type: iterator
size(): It tells us the size of the set.
Parameters: None
Return type: integer - total number of elements in the set
insert(element): Inserts an element in the set.
Time Complexity: O(logN) where N is the size of the set
Parameters: the element to be inserted
Return type: void
erase(value)orerase(start_iterator,end_iterator): Delete elements from the set.
Time Complexity: O(logN) where N is the size of the set
Parameters: the value to be removed or iterators pointing to the position between which the value needs to be deleted
Return type: void
find(element): Returns an iterator pointing to the element, if the element is found else returns an iterator pointing to the end of the set.
Parameters: the element which needs to be found
Return type: iterator
clear(): It deletes all the elements from the set
Parameters: None
Return type: void
empty(): It tells us whether the set is empty or not.
Parameters: None
Return type: Boolean, true if a set is empty else false
#include<iostream>
#include<set>
using namespace std;
int main() {
set <int> s1;
set <int, greater <int>> s2;
for (int i = 0; i < 5; i++) {
s1.insert(i + 1);
}
for (int i = 0; i < 5; i++) {
s2.insert((i + 1) * 10);
}
set <int> ::iterator it;
cout << "Set1 ";
for (it = s1.begin(); it != s1.end(); it++)
cout << * it << " ";
cout << '\n';
cout << "Set2 ";
for (it = s2.begin(); it != s2.end(); it++)
cout << * it << " ";
cout << '\n';
s1.erase(1);
s2.erase(s2.begin(), s2.find(10));
cout << "After erasing element, size of set1 is " << s1.size() << '\n';
int val = 4;
if (s1.find(val) != s1.end())
cout << "The set1 contains " << val << endl;
else
cout << "The set1 does not contains " << val << endl;
cout << "Elements of set1 ";
for (it = s1.begin(); it != s1.end(); it++)
cout << * it << " ";
cout << '\n';
cout << "Elements of set2 ";
for (it = s2.begin(); it != s2.end(); it++)
cout << * it << " ";
cout << '\n';
s1.clear();
if (s1.empty() == true) {
cout << "set1 is empty now!";
}
return 0;
}Output
Set1 1 2 3 4 5
Set2 50 40 30 20 10
After erasing element, size of set1 is 4
The set1 contains 4
Elements of set1 2 3 4 5
Elements of set2 10
set1 is empty now!