Updated April 17, 2023
Introduction to C++ set
C++ sets are special containers for storing unique elements in order. Ordering of the elements should be in a specified manner in the C++ set as it puts most of its emphasis on the key and value pairs of elements. Each element should be different, and once put in the C++ set container cannot be modified as elements will be treated as constant. Although these elements are considered constant and cannot be modified still any new element in the (key and value format) can be inserted and retrieved easily from the container.
Syntax
C++ Set is an associative container concerning other standard library components of C++ where all the elements play an important role. The Syntax flow is represented using the following template :
template < class T_ype,
class Compare = less< T_ype >,
class Alloc = allocator< T_ype >
> class set;
The template includes the following parameters:
- T_ype: It signifies the type of element that is present in the container.
- Compare: A class for comparison is also introduced to take two arguments of the same type and then return a boolean value after comparison. Usage of this is optional in the sense default value will be considered if it is less than the compared values.
- Alloc: Alloc is the class that is specifically used to allocate the values to the storage class and according to the storage class allocator.
How set function work in C++?
A set function is part of the standard library of C++, which is used for storing some unique elements and is then used for performing many operations on top of it. This is a function that allows programmers to use the C++ set easily whenever there is a requirement based on key and value pairs. First, let’s go with the working flow of the C++ function :
- In a set, the value present is the key that is used for accessibility according to the requirement.
- All the elements once present in the container cannot be modified as the value becomes constant.
- Elements in the set don’t allow duplicate elements as the value of key and value pairs.
- The elements can be inserted and retrieved accordingly. However, the only modification is not possible.
- Internally also elements can be sorted, but with some protocols or restrictions like comparisons are made when the internal object present gets compared.
- It is also considered that the set containers are slow when seen and compared with other un_ordered set containers in the sense that the accessibility is slow when set containers are used for iteration, but the other good part is that the set function helps indirect access of the elements.
- They are implemented in a way that represents the binary search tree properly, specifically the red-black tree.
- The associative container makes use of the red-black tree religiously, which has the compare function in it.
- As soon as the iteration and the tree traversal start, it makes use of the compare function, whether it is an object or it is an internal comparator.
- Uniqueness associated is determined using equivalence relation as mentioned in the template when compared with the default value or the precise value in it. If it is less than the actual value present in the container, then the default value will be taken.
- Also, a storage allocator is used for the allocation of storage to the elements related to the storage allocator of the set container in C++.
- The storage allocator mentioned earlier is already present as part of the set template and provides value-independent values, and represents a simple memory allocation model for visibility and enhancement of the container, i.e. the set.
- It uses many methods, constructors, and other items provided with the comparison for the entire functionality.
- Traversal to insertion, retrieval, and iteration everything is simple in terms of implementation when the set of C++ is used as a part of the standard library of C++, and then it is used for functioning of the methods and associated methods. Then, everything becomes simple and flexible for the programmers as per complex requirements.
- The set makes use of some very important methods associated with it, which will be seen further as part of the examples.
Examples
Different examples are mentioned below:
Example #1
This program demonstrates the usage of begin() and ends () method as part of the C++ set, which further represents the String set, charset, and number set with beginning and end function as shown in the output.
Code:
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<string> strng_set{"iphone", "android", "basic", "landline"};
set<char> chr_set{'a', 'b', 'c', 'd'};
set<int> int_set{1, 3, 4, 5};
for (auto kt=strng_set.begin(); kt != strng_set.end(); ++kt)
cout << ' ' << *kt;
for (auto kt=chr_set.begin(); kt != chr_set.end(); ++kt)
cout << ' ' << *kt;
for (auto kt=int_set.begin(); kt != int_set.end(); ++kt)
cout << ' ' << *kt;
return 0;
}
Output:
Example #2
This program demonstrates the size method as part of the C++ set, which is used for getting the size and insert a new set after comparison as shown in the output.
Code:
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<string> strng_set{"iphone", "android", "basic", "landline"};
set<char> chr_set{'a', 'b', 'c', 'd'};
set<int> int_set{1, 3, 4, 5};
for (int i = 0; i < 4; i++) {
int_set.insert('a' + i);
}
cout << "strng_set size: " << strng_set.size();
cout << endl;
cout << "strng_set size: " << chr_set.size();
return 0;
}
Output:
Example #3
This program demonstrates the set::max size function as part of the C++ set, as shown in the output.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
set<int> st_1, st_2;
st_1.insert(4);
st_2.insert(8);
st_1.insert(6);
st_1.insert(5);
cout << st_1.max_size() << endl;
cout << st_2.max_size();
return 0;
}
Output:
Example #4
This program demonstrates the empty method, which returns a boolean value in the form of true or false depending on the presence of an element in the set as shown in the output.
Code:
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> empty_set{1,2,3,4,5};
if (empty_set.empty()) {
cout << "True";
}
else {
cout << "False";
}
return 0;
}
Output:
Conclusion
C++ set plays a very important role like other standard library modules. It helps programmers to play around with the elements to put them easily into order with traversal, manipulation, and retrieval. It provides easy accessibility also when compared with the un_ordered subset in terms of easy accessibility. However, it follows some restrictions still a flexible and versatile method to be used.
Recommended Articles
This is a guide to the C++ set. Here we discuss How to set function work in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –