It depends on what you want to do. So it is clear that you want to use it for some get-the-maximum-values-in-an-iteration construction.
Question 1: Do you access the elements by their keys as well?
If yes, I can think of two solutions for you:
use
boost::bimap
- easy, tested solution with logarithmic runtimes.create a custom container that contains an
std::map
(or for even faster by key accessstd::unordered_map
) and a heap implementation (e.g.std::priority_queue<std::map<key, value>::iterator>
+ custom comparator) as well, keeps them in sync, etc. This is the hard way, but maybe faster. Most operations on both will be O(logn) (insert, get&pop max, get by key, remove) but the constant sometimes do matter. Although is you usestd::unordered_map
the access by key operation will be O(1).You may want to write tests for the new container as well and optimize it for the operation you use the most.
If no, you really just access using elements using the maximum value
Question 2: do you insert/remove/update elements randomly or you first put in all elements in one round and then remove them one by one?
for random insert/remove/updates use
std::priority_queue<std::pair<value, key>>
if you put in the elements first, and then remove them one-by-one, use and
std::vector<std::pair<value, key>>
andstd::sort()
it before the first remove operation. Do not actually remove the elements, just iterate on them.