So, before we move to the internal architecture lets first take a look from top. Before we move forward just wanted to tell there is ABSOLUTELY NO DIFFERENCE between C++ maps and Java maps.
1. Underlying Data Structure:
- map:
- It
is implemented as a self-balancing binary search tree (typically
a Red-Black Tree).
- This
structure ensures that the elements are always sorted based on the keys.
- As
a result, operations like insertion, deletion, and search have a time
complexity of O(log n).
- unordered_map:
- It
is implemented using a hash table.
- The
elements are not stored in any particular order; they are placed in
"buckets" based on the hash value of their keys.
- As
a result, operations like insertion, deletion, and search typically have
an average time complexity of O(1), though in the worst case
(due to hash collisions), it can degrade to O(n).
2. Ordering:
- map:
- The
elements are stored in a sorted order according to the key.
- If
you iterate through a map, you will get the elements in ascending order
of their keys.
- unordered_map:
- The
elements are not stored in any particular order.
- Iterating
through an unordered_map will yield elements in an arbitrary
order, which can vary even between different runs of the same program.
3. Use Cases:
- map:
- Useful
when you need to maintain a sorted order of keys or need to perform range
queries (like finding the first key greater than a given key).
- unordered_map:
- Preferred
when you do not need the keys to be in any particular order and
performance is a concern, as it is generally faster for insertions,
deletions, and lookups due to its average O(1) complexity.
4. Memory Usage:
- map:
- Generally
uses more memory per element because of the tree structure which requires
storing parent-child pointers.
- unordered_map:
- Generally
uses more memory overall due to the hash table's need to handle
collisions and maintain buckets.
5. Exception Handling:
- map:
- The at() function
throws an out_of_range exception if the key is not found.
- unordered_map:
- Similar
to map, the at() function throws an out_of_range exception
if the key is not found, but because of the hashing mechanism, there
could be differences in performance when exceptions are thrown.
Internal Architecture
map (Tree Structure)
Imagine you have a dictionary where all
the words are stored in alphabetical order.
- How
it works:
- When
you add a word, it is automatically placed in the right spot to keep the
dictionary alphabetically sorted.
- If
you look for a word, you start from the middle, then decide whether to go
left or right depending on whether your word comes before or after the
middle word (like a decision tree).
- This
structure is called a binary search tree.
- Key
points:
- Always sorted.
- Slower to
insert or find a word compared to unordered storage because it has to
find the correct place.
- Good
for range-based queries, like finding all words
between "apple" and "banana."
unordered_map (Hash Table)
Now, think of a vending machine where
items are in different slots based on a code (like A1, B2, C3).
- How
it works:
- When
you want to store an item (key-value pair), you use a function (called
a hash function) to decide which slot it should go into (like
a code).
- When
you look for an item, you use the same hash function to directly go to
that slot.
- This
is called a hash table.
- Key
points:
- Not
sorted; items are just placed in slots.
- Faster on
average to insert or find an item because you go straight to its slot.
- Not
good for ordering or finding a range of items,
just for quick lookups.
Simple Comparison:
- smap is
like an alphabetically sorted dictionary. Good for
keeping things in order but slower to add or find items.
- unordered_map is
like a vending machine with specific codes for each
item. It’s fast to find what you want, but there’s no order to the items.
This way, map is organized and sorted,
while unordered_map is designed for speed and random access!
1. Leetcode Problem : Integer to English Words
2. LeetCode Problem : Robot Collisions
If I remember any other questions, I will add Comments, So You can Check Comment Section of this Blog :)
If I remember any other questions, I will add Comments, So You can Check Comment Section of this Blog :)
Tags
Data Structure