Level up your coding skills with
the C++ Standard Template
Library (STL):
Sets
BY JOYJIT CHOUDHURY
Unordered Set
 The unordered_set is a container that stores unique
elements in no particular order. It allows fast retrieval of
individual elements based on their value.
 It is typically implemented as a hash table, where the
value of the element is also it’s key
 Search, insertion, and removal have an average time
complexity of O(1)
 Defined in the header <unordered_set>
 Belongs to the namespace std
(remember : using namespace std)
Insertion of elements
uSet
uSet.insert(12);
uSet
12
uSet
12
uSet.insert(24);
24
Insertion of elements
uSet
12
uSet.insert(24);
24
uSet
12 24
uSet
12 24
uSet.insert(12);
uSet.insert(36);
uSet
12 24
36
Remove elements
uSet.erase(24);
uSet
12 24
36
uSet
12
36
Remove elements
uSet
12
uSet
12
uSet
12
uSet.erase(36);
Search for an element
 Member function find(), searches the container for an
element with a particular value. If such an element exists, it
returns an iterator to that element. Otherwise it returns an
iterator to unordered_set::end (the element past the end of
the container)
 Another function count(), can be used to check whether
an element exists or not. It returns 1, if the desired element
exists, otherwise it returns 0
Number of elements
 Member function size(), returns the number of elements in the unordered set
Is it empty?
 Member function empty(), returns true if the unordered set is empty, false
otherwise
Set
 The set is a container that stores unique elements in a particular
order. Sorting is done using the comparison function compare
 It is typically implemented as a balanced binary search tree, where
the value of the element is also it’s key
 Search, insertion, and removal have an average time complexity of
O(logN)
 Defined in the header <set>
 Belongs to the namespace std
(remember : using namespace std)
Set
 Search, insertion, and removal of elements with respect to set can
be done in the same way as that for unordered_set using the
functions insert(), erase(), find().
 The difference being, the different time complexities of these
operations with respect to the different containers
O(1) : unordered_set vs. O(logN) : set
 Member functions size(), empty() also perform the same actions for
set as they do for unordered_set
 The elements in set are stored in a particular order and allow range
based iterations, whereas unordered_set stores the elements in a
random order
 That’s not all. There are many other functions
and techniques that could come in handy.
Read about them on cplusplus.com or
cppreference.com or somewhere else. Just
google it !
Sets

Sets

  • 1.
    Level up yourcoding skills with the C++ Standard Template Library (STL): Sets BY JOYJIT CHOUDHURY
  • 2.
    Unordered Set  Theunordered_set is a container that stores unique elements in no particular order. It allows fast retrieval of individual elements based on their value.  It is typically implemented as a hash table, where the value of the element is also it’s key  Search, insertion, and removal have an average time complexity of O(1)  Defined in the header <unordered_set>  Belongs to the namespace std (remember : using namespace std)
  • 3.
  • 4.
    Insertion of elements uSet 12 uSet.insert(24); 24 uSet 1224 uSet 12 24 uSet.insert(12); uSet.insert(36); uSet 12 24 36
  • 5.
    Remove elements uSet.erase(24); uSet 12 24 36 uSet 12 36 Removeelements uSet 12 uSet 12 uSet 12 uSet.erase(36);
  • 7.
    Search for anelement  Member function find(), searches the container for an element with a particular value. If such an element exists, it returns an iterator to that element. Otherwise it returns an iterator to unordered_set::end (the element past the end of the container)  Another function count(), can be used to check whether an element exists or not. It returns 1, if the desired element exists, otherwise it returns 0
  • 9.
    Number of elements Member function size(), returns the number of elements in the unordered set
  • 10.
    Is it empty? Member function empty(), returns true if the unordered set is empty, false otherwise
  • 11.
    Set  The setis a container that stores unique elements in a particular order. Sorting is done using the comparison function compare  It is typically implemented as a balanced binary search tree, where the value of the element is also it’s key  Search, insertion, and removal have an average time complexity of O(logN)  Defined in the header <set>  Belongs to the namespace std (remember : using namespace std)
  • 12.
    Set  Search, insertion,and removal of elements with respect to set can be done in the same way as that for unordered_set using the functions insert(), erase(), find().  The difference being, the different time complexities of these operations with respect to the different containers O(1) : unordered_set vs. O(logN) : set  Member functions size(), empty() also perform the same actions for set as they do for unordered_set  The elements in set are stored in a particular order and allow range based iterations, whereas unordered_set stores the elements in a random order
  • 17.
     That’s notall. There are many other functions and techniques that could come in handy. Read about them on cplusplus.com or cppreference.com or somewhere else. Just google it !