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

Maps

Embed Size (px)

Citation preview

Page 1: Maps

Level up your coding skills with

the C++ Standard Template

Library (STL):

Maps

BY JOYJIT CHOUDHURY

Page 2: Maps

Unordered Map

unordered_map is an associative container that contains key-value pairs with

unique keys. It allows fast retrieval of individual elements based on their keys.

Internally, the elements in the unordered_map are not sorted in any particular

order with respect to either their key or mapped values

Search, insertion, and removal have an average time complexity of O(1)

It is typically implemented as a hash table

Defined in the header <unordered_map>

Belongs to the namespace std

(remember : using namespace std)

Page 3: Maps

Insert elements

Page 4: Maps

Erase elements

Page 5: Maps

Lookup an element

Page 6: Maps

Access and Modify an element

Page 7: Maps

Map

map is a sorted associative container that contains key-value pairs

with unique keys. Keys are sorted by using the comparison function compare

Search, insertion, and removal have an average time complexity of

O(logN)

It is typically implemented as a balanced binary search tree

Defined in the header <map>

Belongs to the namespace std

(remember : using namespace std)

Page 8: Maps

Map Search, insertion, and removal of elements with respect to map can

be done in the same way as that for unordered_map using the

functions insert(), erase(), find() and operator[]

The difference being, the different time complexities of these

operations with respect to the different containers

O(1) : unordered_map vs. O(logN) : map

The elements in map are stored in a particular order and allow range

based iterations, whereas unordered_map stores the elements in a

random order

Page 9: Maps
Page 10: Maps
Page 11: Maps
Page 12: Maps
Page 13: Maps

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 14: Maps