Do you think IIT Guwahati certified course can help you in your career?
No
What is Set in C++?
Set in C++ is a container from the Standard Template Library (STL) that stores unique elements in a specific order, typically in ascending. It automatically manages duplicates, ensuring no two elements are the same. Sets support efficient element insertion, deletion, and searching, with operations having logarithmic time complexity due to their underlying binary search tree structure.
Syntax of Set in C++
set<datatype> nameofset;
Example for declaration of Set in C++
set<int> value = {6, 1, 52, 13};
Properties of Set in C++
A set's values are not indexed.
Each element in a set has a distinct value.
The element's value cannot be changed after it has been added to the set, but it is possible to remove the element and then re-add the modified value. As a result, the values are unchangeable.
Elements in the set are stored in the sorted order.
Ordered Set
Unordered Set
It contains the unique elements and elements are stored in sorted order by default.
Elements are not in sorted order
Implementation using Balanced BST
Implemented using hashing
Present in #include<set>
Present in #include<unordered_set>
Basic functions associated with Set in C++
insert()
emplace()
Used to insert an element in the set
s.insert(value)
s.emplace(value)
size()
Tells the size of set return integer value
s.size()
empty()
Tells whether the set is empty or not
s.empty()
clear()
Removes all the elements of set
s.clear()
find()
Will return the iterator where that element is present in the set
auto it = s.find(value)
erase()
Will delete that particular element in the set and will do the same in logarithmic time
s.erase(value)
begin()
It returns the iterator for the first element in the set
auto it = s.begin()
end()
It returns the iterator for the last element in the set
auto it = s.end()
rbegin()
It returns the reverse iterator.It points to the last element of set
auto it = s.rbegin()
rend()
It points to the first element of the set and it is the reverse operator.
auto it = s.rend()
lower_bound(value)
Returns an iterator to the first element in the set that is either equivalent to 'value' or will not go before the element 'value'.
auto it = s.lower_bound(value)
upper_bound(value)
Returns an iterator to the first element in the set that will come after the element 'value'
auto it = s.upper_bound(value)
Implementation:
// Implementation of Basic functions associated with Set in C++
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int>s;
s.insert(1); // {1}
s.emplace(2); // {1, 2}
s.insert(2); // {1, 2}
s.insert(4); // {1, 2, 4}
s.insert(3); // {1, 2, 3, 4}
// Printing size of set
cout<<s.size()<<endl;
// Printing elements of set
for(auto i : s){
cout<<i<<" ";
}
cout<<endl;
// {1, 2, 3, 4, 5}
auto it = s.find(3);
cout<<*it<<endl;
// {1, 2, 3, 4, 5}
// auto it = s.find(6);
// {1, 4, 5}
s.erase(5); // erases 5 // takes logarithmic time
int cnt = s.count(1);
// auto it = s.find(3);
// s.erase(it); // it takes constant time
// {1, 2, 3, 4, 5}
auto it1 = s.find(2);
auto it2 = s.find(4);
// after erase {1, 4, 5} [first, last)
s.erase(it1, it2);
/*
lower_bound() and upper_bound() function works in the same way
as it works in case of vectors
This is the syntax
auto it = s.lower_bound(2);
auto it = s.upper_bound(3);
*/
}
You can also try this code with Online C++ Compiler
#include <bits/stdc++.h>
using namespace std;
int main() {
// Everything is same as set in multiset only thing is that it stores the duplicate values
multiset<int> m;
m.insert(1); // {1}
m.insert(1); // {1, 1}
m.insert(1); // {1, 1, 1}
// Size of multiset
cout<<m.size()<<endl;
// Print all the elements
for(auto i:m)cout<<i<<endl;
// all 1's erased from the multi set
m.erase(1);
int count = m.count(1);
// only a single one erased
m.erase(m.find(1));
// rest all function will work same as set explained above
}
You can also try this code with Online C++ Compiler
How can we store the elements in descending order in sets?
We can declare the set in this way set<int, greater<int> > s;
What is the time complexity to find and delete an element in the set?
log(n) where n is the total number of elements in the set.
What is the real-life application of the set?
We can use a set in those places where we have a requirement that every entity which we store in the set has only one occurrence. Suppose we have to store the details of a student and we know that two students with the same details are not possible. Hence we can use a set to store the students.
Conclusion
In this article, we have extensively discussed the theory of Set in C++ along with the implementation of some functions in C++. Until then, All the best for your future endeavors, and Keep Coding. We hope that this blog has helped you enhance your knowledge regarding the set in C++, and if you would like to learn more, check out our articles on Code360. Do upvote our blog to help other ninjas grow.