# A Set Data Structure (STL) In C++

**Introduction**

In this post, we will discover what a set is, when and how to utilize it, as well as how it functions internally and the many set actions. With the help of examples, we will learn about the many STL functions that may be applied to the set. In C++, a set is just a standard template library (STL) container that is used anytime we need to keep distinct items (no duplicate values) and store them in a certain order. The set's elements can be added or deleted, but once they are added, they cannot be changed.

**Syntax and Definition**

To define a set, we must first utilize the STL set, then declare the data type of the set's elements in the angle brackets ( <>), followed by the set's name.

If you want the elements to be sorted in descending order instead of ascending order, you must add greaterdatatype > along with the data type. By default, the set maintains the elements in ascending order.

**Syntax:**

Since each value in the set serves as a key and the set doesn't enable indexing, each element of the set is distinct, meaning that no duplicate values may be placed in it. Since there can only be one index, the elements/values (keys) themselves are the indexes. Additionally, just the keys and values must be used to access the values in the set. The components of a set are also kept in sorted order. Whether the components are sorted in ascending or descending order may be specified by altering the syntax during the set definition.

In logarithmic time complexity, the elements in the set may be added, deleted, and searched. As soon as an element is added to a set, it becomes a constant and cannot be changed (its value cannot be altered). The binary search tree in C++ internally implements the set STL.

**Begin and End functions of Set**

**Insert, Delete and Swapping Operations**

**find(), count(), upper_bound(), lower_bound() Functions**

**Things to Remember**

- In C++, a set is a container from the standard template library (STL). The set's elements are distinct, ordered, and immutable.
- The STL set should be used first when defining a set, followed by the name of the set and the data type of its components in angle brackets ( >).
- The set holds the entries by default in descending order. For descending order, use greaterdatatype> in conjunction with the data type.
- The BSTs in C++ serve as the internal implementation of the set STL.
- Use the insert function and the set name to insert data: name of set.insert(data);
- Erase function with the set name and the location(s) in the form of an iterator to delete data (s). name of set.erase(iterator);
- Operations like begin, end, size, and empty in a set require O(1) time.
- O(Log N)-time operations include insert, find, count, and upper bound and lower bound
- In O(N) time, operations like erase and clear
- In C++, operations like insertion and deletion in the set typically have a time complexity ofO(log n)

### Subscribe to our Newsletter

Receive latest industry news and updates, exclusive offers directly in your inbox.