Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding top elements of a map using Heap

Tags:

c++

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

like image 370
deevee Avatar asked Jan 28 '26 07:01

deevee


1 Answers

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

like image 144
sehe Avatar answered Jan 30 '26 20:01

sehe