Finding top elements of a map using Heap
I have a unordered_hashmap
that maps a string (say personName or SSN) to a struct
Attributes
that has many attributes of that person including annualIncome
. There are many such hash maps corresponding t开发者_Python百科o different organizations such as mapOrganizationA
, mapOrganizationB
etc. I need to find the people (with attributes) with the top-k annual incomes. I was thinking of using a min-heap with k-nodes (with the minimum salary as root), so that I can scan the maps one by one, of the current element has income more than the root of the min-heap, the root can be updated. Is this the right approach to get top-k from different maps? Is there a min-heap
datastucture in STL I can make use of.
You can use make_heap
, push_heap
, pop_heap
, sort_heap
, is_heap
to treat any non-associative container (or sequence, really) as a heap.
That would not fit you map nicely, but I assume nothing would prevent you from storing the values (or pointers/references to those) inside, say, a list for this purpose?
Also, perhaps look at Boost.MultiIndex which is a library precisely focused on providing multiple (efficient!) indexes on the same data
精彩评论