Sets in C++ Example | C++ Sets Program
Sets in C++ Example | C++ Sets Program is today’s topic. Sets are associative containers that contain unique objects because the value of the elements identifies the container. They store unique elements in a particular order. The value of the elements in a set cannot be modified once they are put into the container, but other operations such as removal and insertion on the container can take place.
Sets in C++ Example
Content Overview
The application of sets is mainly in binary search trees.
In the sets, we access elements with the help of key which is identified.
Sets containers are slower than the unordered_sets containers when it comes to accessing individual elements by their key, but the main advantage with sets is that they allow the direct iteration on the subsets based on their order of items in the container.
Definition for the container
template < class T, class Compare = less<T> Class Allocation= allocator<T> > class set;
Here class T set::key_value (Here T can take any data type value)
In next line we compare the key set::key_compare/value_compare
In last we allocate i.e set::allocator_type
Parameters
Key (T) – Type of the element contained. The key must be substituted by any other data type, including user-defined data type.
Container properties
1- Associative: Elements present in the containers are generally referenced by their key and not by their absolute position in the container.
2- Ordered: The elements in the container follow a strict order, i.e. all the elements inserted in this set are given a position in the order.
3– Set: The value of an element is also the key used to identify it.
4- Unique keys: No two items in the container can have the same keys.
5- Allocator-aware: The container uses an allocator object to meet its storage needs.
Different functions associated with sets
1- begin() – It returns an iterator to the first element present in the set.
2- end()- It returns the iterator to the last element present in the set.
3- size()– It returns the numbers of elements present in the set.
4- max_size()– It returns the maximum number of items that a particular set can hold at a time.
5- empty()- It checks and returns if the set is empty or not.
6- rbegin()– It returns a reverse iterator pointing to the last element present in the container.
7- rend()– It returns a reverse iterator, which points to the theoretical element right before the first element in the set container.
8- crend() – It returns a constant iterator which points to the position just before the first element in the container.
9- crbegin()– It returns a constant iterator which points to the last element of the container.
10- cbegin() – It returns a constant iterator pointing to the first element in the container.
11- cend()– It returns a constant iterator which points to the position past the last item in the container.
12- insert(const k)– It adds a new item ‘k’ to the set.
13-iterator insert(iterator position, const g) – It adds a new element ‘g’ at the position pointed by the iterator.
14- erase(iterator position)– It removes the item at the position pointed by the iterator.
15- erase(const k)– It removes the value ‘k’ from the set.
16- clear() – It removes all the elements from the set.
17- find(const k)– It returns an iterator to the item ‘k’ in the set if found, else returns an iterator to end.
18- count(const k ) – It returns 1 or 0 based on the element ‘k’ present in the set or not.
19-lower_bound(const k)– It returns an iterator to the first element that is equivalent to ‘k’ or definitely will not be there before the element ‘k’ in the set.
20 – upper_bound(const k)– It returns an iterator to the first element that is equivalent to ‘k’ or definitely will go after the item ‘k’ in the set.
There are different other functions also present for defining the set.
C++ Set Program
Q1- Write the program to insert an element in a set.
#include <iostream> #include <set> using namespace std; int main() { set<int> set1; set<int>::iterator it = set1.begin(); set<int>::iterator iterator_1, iterator_2; set1.insert(20); set1.insert(202); set1.insert(230); set1.insert(240); set1.insert(250); //printing element after insertion cout << "Elements after insertion = "; for (iterator_1 = set1.begin(); iterator_1 != set1.end(); ++iterator_1) cout << *iterator_1 << " "; }
See the output.
Q2- Write a program to insert five elements in a set and then delete 2 elements and then print the set.
#include <iostream> #include <set> using namespace std; int main() { set<int> set1; set<int>::iterator it = set1.begin(); set<int>::iterator iterator_1, iterator_2; set1.insert(20); set1.insert(202); set1.insert(230); set1.insert(240); set1.insert(250); //printing element after insertion cout << "Elements after insertion = "; for (iterator_1 = set1.begin(); iterator_1 != set1.end(); ++iterator_1) cout << *iterator_1 << " "; //deleting the elements set1.erase(20); set1.erase(202); // printing set elements after deletion cout << "\nElement after deletion : "; for (iterator_1 = set1.begin(); iterator_1 != set1.end(); ++iterator_1) cout << *iterator_1 << " "; }
See the output.
Conclusion
Sets are the type of associative containers in which each item has to be unique because the value of the item identifies it.
The value of an item cannot be modified once it is added to a set, though it can be removed and add the modified value of that item.
Finally, Sets in C++ Example Tutorial is over.