How to choose between map and unordered map

0 votes
Assume I wanted to map data that had a string as the key.

Should I have used map or unordered map as my container?

Because unordered map consumes more memory, let's pretend memory isn't a problem and the only concern is performance.

The average complexity of unordered map should be O(1), with the worst case being O(2) (n).

In what circumstances would it reach O(n)?

When does a map become faster than unordered map?

Is this true when n is small?

Assuming I use STL unordered map with the default haser Vs. map, the key is string.

Should I use map if I'm going to loop through the items rather than accessing an individual element each time?
Jul 4, 2022 in C++ by Nicholas
• 7,760 points
626 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
If memory is not a problem, unordered map is always quicker for single element access.

The worst-case scenario is theoretical and limited to a single hash that accounts for all of the components.

This has no practical application.

When there are at least log N entries belonging to the same hash, the unordered map becomes slower.

This is also irrelevant in practise.

In some cases, a specialised hashing technique that assures a more equal distribution might be used.

The generic hash functions included with unordered map are just as good for regular strings that don't share a specific pattern.

You cannot use unordered map if you wish to explore the map in a sorted manner (using iterators).

Map, on the other hand, not only supports this, but may also offer the next element in a map based on an estimate of the key (see lower bound and upper bound methods).
answered Jul 5, 2022 by Damon
• 4,960 points

edited Mar 5

Related Questions In C++

0 votes
1 answer

How to use new[ ] and delete[ ] operator in C++

int main(){ char *str; ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
686 views
0 votes
0 answers

What is the difference between std::list<std::pair> and std::map in C++ STL?

What distinguishes std::list<std::pair> from std::map? Does the ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
860 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
796 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
649 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,317 views
0 votes
0 answers

How to traverse stack in C++?

Is traversing std::stack possible in C++? It is not possible to traverse using the following method.  Because there is no member end in std::stack. std::stack<int> foo; // .. for (__typeof(foo.begin()) it = foo.begin(); ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
2,431 views
0 votes
1 answer

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
749 views
0 votes
1 answer

please help me with max_element function in c++ stl

You can substitute max for *max eleme ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
903 views
0 votes
0 answers

How to find if a given key exists in a C++ std::map

I'm trying to check if a given ...READ MORE

Jul 14, 2022 in C++ by Nicholas
• 7,760 points
857 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
3,173 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP