Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL priority_queue<pair> vs. map

I need a priority queue that will store a value for every key, not just the key. I think the viable options are std::multi_map<K,V> since it iterates in key order, or std::priority_queue<std::pair<K,V>> since it sorts on K before V. Is there any reason I should prefer one over the other, other than personal preference? Are they really the same, or did I miss something?

like image 529
Baruch Avatar asked Jan 12 '15 09:01

Baruch


People also ask

What is the difference between pair and map?

std::pair is just for grouping together exactly 2 objects (say, "coordinates on a page" is comprized of X and Y). std::map is a mapping from one set of objects to another.

Which is faster vector or map?

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 ...

Is a priority queue a map?

A priority queue is a concept like a list or a map; just as a list can be implemented with a linked list or with an array, a priority queue can be implemented with a heap or with a variety of other methods such as an unordered array.

What is the use of map in STL?

Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values.


1 Answers

A priority queue is sorted initially, in O(N) time, and then iterating all the elements in decreasing order takes O(N log N) time. It is stored in a std::vector behind the scenes, so there's only a small coefficient after the big-O behavior. Part of that, though, is moving the elements around inside the vector. If sizeof (K) or sizeof (V) is large, it will be a bit slower.

std::map is a red-black tree (in universal practice), so it takes O(N log N) time to insert the elements, keeping them sorted after each insertion. They are stored as linked nodes, so each item incurs malloc and free overhead. Then it takes O(N) time to iterate over them and destroy the structure.

The priority queue overall should usually have better performance, but it's more constraining on your usage: the data items will move around during iteration, and you can only iterate once.

If you don't need to insert new items while iterating, you can use std::sort with a std::vector, of course. This should outperform the priority_queue by some constant factor.

As with most things in performance, the only way to judge for sure is to try it both ways (with real-world testcases) and measure.

By the way, to maximize performance, you can define a custom comparison function to ignore the V and compare only the K within the pair<K,V>.

like image 195
Potatoswatter Avatar answered Oct 21 '22 14:10

Potatoswatter