Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vector or map, which one to use?

I've heard many people say that if the number of expected elements in the container is relatively small, it is better to use std::vector instead of std::map even if you were to use the container for lookups only and not iterating.

What is the real reason behind this?

Obviously the lookup performance of std::map cannot be worse than std::vector (although it may differ in nanoseconds/microseconds) so does it have something to do with memory usage?

Does std::vector fare any better/worse than std::map in fragmenting the virtual address space?

I am using the STL library that comes along with Visual Studio (i.e. Microsoft's implementation). Does that make any difference compared to other implementations?

like image 459
Naveen Avatar asked Jan 18 '09 07:01

Naveen


People also ask

Is a vector a map?

A vector map, like OS MasterMap, is basically a database of points, lines and polygons which collectively make up all the features on the map. It's possible to assign each of these features extra information – perhaps demographic data and the age of the buildings for example.

Should I use vector or array?

Vector is a dynamic array. You may use array when you assume a little data or you are sure that you would take as much data as your array size is. In array the size is predefined so it takes memory as well. On the other hand you may use Vector when you need to assume such size of data.

Can we have vector in map?

Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. Map of Vectors in STL: Map of Vectors can be very efficient in designing complex data structures.

Is unordered map faster than vector?

std::unordered_map will be the fastest and use the most memory. Use it if you want a hashtable.


3 Answers

I presume you're comparing map<A, B> with vector<pair<A, B> >.

Firstly, finding an item in a very small vector can easily be faster than the same thing in a map, because all the memory in a vector is always contiguous (and so plays more nicely with computers' caches and such things), and the number of comparisons needed to find something in a vector might be about the same as for a map. Finding an element in a map needs fewer operations in the limit of very large containers.

The point where maps become faster than vectors depends on the implementation, on your processor, what data is in the map, and subtle things like what memory is in the processor's cache. Typically, the point where map becomes faster would be about 5-30 elements.

An alternative is to use a hash container. They are often named hash_map or unordered_map. Classes named hash_map are not part of the official standard (and there are a few variants out there); std::tr1::unordered_map is. A hash map is often faster than a normal map for lookups, regardless of how many elements are in it, but whether it is actually faster depends on what the key is, how it is hashed, what values you have to deal with, and how the key is compared in std::map. It doesn't keep things in a specific order like std::map, but you've said that you don't care about that. I'd recommend hash maps particularly if the keys are integers or pointers, because these hash very quickly.

like image 159
Doug Avatar answered Oct 12 '22 21:10

Doug


"By default, use vector when you need a container" - Bjarne Stroustrup.

Otherwise, I find this little flow chart to be of very good help (edited - probably a valid live new link):

https://ngoduyhoa.blogspot.com/2015/06/summary-of-different-containers.html

like image 30
Stefan Rådström Avatar answered Oct 12 '22 22:10

Stefan Rådström


Maps are usually implemented as binary search trees, and walking a binary tree always comes with a little overhead (performing comparisons, walking links, etc.) Vectors are basically just arrays. For very small amounts of data, maybe 8 or 12 elements, sometimes it's faster just to do a linear search over an array than to walk a binary search tree.

You can run some timings yourself to see where the break-even point is -- time a search over four elements, then eight, then sixteen, and so on to find the sweet spot for your particular implementation of the STL.

Maps do tend to have a bunch of small allocations all over the heap, whereas vectors are contiguous so the cache-hit rate of vectors can sometimes be a little better in cases where you're iterating over all the elements from front to back.

like image 29
Crashworks Avatar answered Oct 12 '22 22:10

Crashworks