Sets and Multisets in C++
Sets and Multisets in C++
Group members ID
Section: C
Sets in C++ are a kind of STL containers that are used for storing elements in a
sorted way. The elements are ordered to a strict weak ordering in a set type container
and since this elements are constant we cannot manipulate the values of the existing
elements in the set. Sets offer high performance sorting, ordering, and membership
testing, sets underpin solutions for diverse problems. This essay will dive deep into C++
sets - their technical internals, syntax, capabilities, use cases, and more. multisets
permit duplicates. These containers are implemented as ordered binary trees, ensuring
efficient searching, insertion, and deletion operations.
Ordered sets implement a red-black tree structure internally. The elements are sorted
according to a comparator function, allowing rapid insert, delete, and search in
logarithmic O(log n) time.
Unordered sets use hashing with buckets, giving average O(1) time but losing ordering.
Properties of sets
● Internally, set containers are implemented as red-black trees which enables efficient
sorting and searching in O(log n) time.
● Elements are ordered using the < comparison operator which must be defined for the
element type.
● It maintains the ordering by inserting elements in the correct position based on the
comparator.
● Duplicate elements are not allowed in std::set but allowed in std::multiset.
● When you need to store unique/sorted data and access it efficiently. The automatic
sorting and removal of duplicates is convenient.
● When you need fast search, insert, delete operations on sorted data.
● As an alternative to std::map when you just need to store keys but not key-value pairs.
● When you need a sorted collection of elements but don't want to call sort() explicitly
whenever elements are added/removed.
Some applications
● Storing sorted lists of unique elements like names, grades, etc.
● Membership testing - fast checking if element exists already or not.
● Mathematical set operations like unions, intersections, differences etc.
● Removing duplicates efficiently from a collection.
Sets are traversed using iterators, so we must include the needed header files to work
with sets which could be
Syntax
Set containers are typically employed when you need to store a collection of unique elements,
ensuring that no duplicates exist. On the other hand, multiset containers allow duplicate
elements, making them suitable for scenarios where multiple occurrences of the same value are
expected.
Common Operations
Insertion: Adding elements to a set or multiset can be done using the insert() member function.
We define the type of values to be stored in a set at the time of initialization in C++. with
default sorting from smallest to largest. Sets can be initialized in many kinds of ways as well
with some being:
● Empty set
● set with values
● set initialized with another set
● set initialized with an array
In C++, iterators are essential for traversing and manipulating elements in various containers,
including sets and multisets. For sets and multisets, we primarily use begin() and end() iterators.
Iterator Usage
Traversal: To iterate through the elements in a set or multiset, use a for loop with iterators.
Insertion with Iterators: you can insert elements at a specific position using iterators