次のステートメントをどこかで読んでください。
追加のハッシュ テーブルを使用して、最小ヒープでの削除を高速化できます。
priority_queue
質問>とを組み合わせunordered_map
て上記のアイデアを実装するにはどうすればよいですか?
#include <queue>
#include <unordered_map>
#include <iostream>
#include <list>
using namespace std;
struct Age
{
Age(int age) : m_age(age) {}
int m_age;
};
// Hash function for Age
class HashAge {
public:
const size_t operator()(const Age &a) const {
return hash<int>()(a.m_age);
}
};
struct AgeGreater
{
bool operator()(const Age& lhs, const Age& rhs) const {
return lhs.m_age < rhs.m_age;
}
};
int main()
{
priority_queue<Age, list<Age>, AgeGreater> min_heap; // doesn't work
//priority_queue<Age, vector<Age>, AgeGreater> min_heap;
// Is this the right way to do it?
unordered_map<Age, list<Age>::iterator, HashAge > hashTable;
}
質問> 以下の作業ができません。
priority_queue<Age, list<Age>, AgeGreater> min_heap; // doesn't work
リストをコンテナーとして使用する必要があります b/c リストの反復子は挿入/削除の影響を受けません (反復子の無効化規則)