Introduction to C++ STL Set
The C++ Standard Template Library (STL) provides a powerful set of container classes and algorithms that greatly simplify the process of programming in C++. One of the most commonly used containers in the STL is the “set” container. In this article, we will explore the features and usage of the C++ STL set, along with some examples to illustrate its functionality.
Overview of C++ STL Set
The set container in C++ STL is an ordered collection of unique elements. It stores its elements in a sorted order, allowing efficient search, insertion, and deletion operations. The elements in a set are automatically sorted when they are inserted, and duplicate elements are not allowed.
The set container is implemented as a binary search tree, which ensures that the elements are always sorted. This makes it suitable for scenarios where maintaining a sorted collection of unique elements is required.
Declaration and Initialization of a Set
To use the set container in C++, you need to include the <set>
header file. Here’s an example of how to declare and initialize a set:
#include <set>
using namespace std;
int main() {
set<int> mySet; // Declaration of an empty set of integers
// Inserting elements into the set
mySet.insert(10);
mySet.insert(20);
mySet.insert(30);
return 0;
}
In the above example, we declare an empty set called “mySet” that stores integers. We then insert three elements into the set using the insert()
function.
Operations on C++ STL Set
The C++ STL set provides a wide range of operations to manipulate and access its elements. Some of the common operations include:
Insertion
The insert()
function is used to insert elements into a set. It takes the value to be inserted as an argument and automatically maintains the sorted order of the set. Duplicate values are ignored.
mySet.insert(40); // Inserting a new element into the set
Deletion
The erase()
function is used to remove elements from a set. It can take either a specific value or an iterator pointing to the element to be deleted.
mySet.erase(20); // Removing a specific element from the set
Searching
The find()
function is used to search for a specific element in a set. It returns an iterator pointing to the element if found, or the end iterator if the element is not present.
auto it = mySet.find(30); // Searching for an element in the set
if (it != mySet.end()) {
// Element found
} else {
// Element not found
}
Size and Empty Check
The size()
function is used to determine the number of elements in a set, while the empty()
function checks whether the set is empty or not.
int setSize = mySet.size(); // Getting the size of the set
bool isEmpty = mySet.empty(); // Checking if the set is empty
Example: Finding Unique Elements in an Array
Let’s consider an example where we have an array of integers and we want to find the unique elements in the array using a set:
#include <set>
#include <vector>
#include <iostream>
using namespace std;
vector<int> findUniqueElements(const vector<int>& arr) {
set<int> uniqueSet;
for (int num : arr) {
uniqueSet.insert(num);
}
vector<int> uniqueElements(uniqueSet.begin(), uniqueSet.end());
return uniqueElements;
}
int main() {
vector<int> arr = {10, 20, 30, 10, 40, 20, 50};
vector<int> uniqueElements = findUniqueElements(arr);
cout << "Unique Elements: ";
for (int num : uniqueElements) {
cout << num << " ";
}
cout << endl;
return 0;
}
In the above example, we define a function called findUniqueElements()
that takes an array as input and returns a vector containing the unique elements. We use a set to store the unique elements and then convert it back to a vector for convenience.
The output of the above program will be:
Unique Elements: 10 20 30 40 50
Conclusion
The C++ STL set is a powerful container that provides an efficient way to store and manipulate a collection of unique elements in a sorted order. It offers a wide range of operations for insertion, deletion, and searching, making it a versatile tool for various programming scenarios. By understanding the features and usage of the set container, you can enhance your C++ programming skills and write more efficient code.