Why do we even use unordered map if ordered map has the same functionality and it is ordered which is even better data accesibility?
Unordered map vs ordered map
mostly because , of search time , in unordered map average search time is o(1) as compared to map which has an average search time of o(logn)
FOR MAP
ordering : - increasing order (by default)
Implementation : Self Balancing BST
search time : logn
insertion time : logn + rebalancing
deletion time : logn + rebalancing
FOR UN-ORDERED MAP
ordering : - no ordering
Implementation : Hash Table
search time : o(1) (Average) || o(n) (worst case)
insertion time : same as search time
deletion time : same as search time
so,
Use std::map when
- You need ordered data.
- You would have to print/access the data (in sorted order).
- You need predecessor/successor of elements.
Use std::unordered_map when
- You need to keep count of some data (Example – strings) and no ordering is required.
- You need single element access i.e. no traversal.
In case of any doubt feel free to ask
Mark your doubt as resolved if you got your answer
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.