18
Level up your coding skills with the C++ Standard Template Library (STL): Sets BY JOYJIT CHOUDHURY

Sets

Embed Size (px)

Citation preview

Page 1: Sets

Level up your coding skills with

the C++ Standard Template

Library (STL):

Sets

BY JOYJIT CHOUDHURY

Page 2: Sets

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)

Page 3: Sets

Insertion of elements

uSet

uSet.insert(12);

uSet

12

uSet

12

uSet.insert(24);

24

Page 4: Sets

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

Page 5: Sets

Remove elements

uSet.erase(24);

uSet

12 24

36

uSet

12

36

Remove elements

uSet

12

uSet

12

uSet

12

uSet.erase(36);

Page 6: Sets
Page 7: Sets

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

Page 8: Sets
Page 9: Sets

Number of elements

Member function size(), returns the number of elements in the unordered set

Page 10: Sets

Is it empty?

Member function empty(), returns true if the unordered set is empty, falseotherwise

Page 11: Sets

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)

Page 12: Sets

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

Page 13: Sets
Page 14: Sets
Page 15: Sets
Page 16: Sets
Page 17: Sets

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 !

Page 18: Sets