を使用して記述された関数がありますが、std::multimap
これは正確にstd::multimap
. 分析の結果、 をheapstd::multimap
としてのみ使用していることに気付きました。そのため、この使用法がより高速になることを期待して、ヒープ操作のみを許可する に置き換えようとしています。std::priority_queue
もちろん、 の要素の型は でstd::priority_queue
ある必要があり、その後、ペアの最初の値 ( の実際のキー) のみをstd::pair<mmKey, mmValue>
比較するカスタム コンパレータを に渡します。std::priority_queue
std::multimap
すべてが非常にテンプレート化されているため、私は..かなり迷子になり、助けが必要です. サンプルコードを作成しました:
の例std::multimap
template <typename Compare>
void littleTestFunction(Compare comp){
std::multimap<int,int,Compare> testMap(comp);
testMap.insert(std::make_pair(1,5));
testMap.insert(std::make_pair(1,6));
testMap.insert(std::make_pair(2,7));
for (; !testMap.empty(); ){
std::cout << testMap.begin()->second << std::endl;
testMap.erase(testMap.begin());
}
return;
}
int main(void){
littleTestFunction(std::less<int>());
}
をに変換する私のstd::multimap
試みstd::priority_queue
template <typename Compare>
class pqcomparison{
private:
Compare myComp;
public:
pqcomparison(Compare &cmp){
myComp = cmp;
}
bool operator() (const std::pair<int, int> &lhs,
const std::pair<int, int> &rhs){
return myComp(rhs.first, lhs.first);
}
};
template <typename Compare>
void littleTestFunction(Compare comp){
std::priority_queue<std::pair<int, int>,
std::vector<std::pair<int, int> >,
pqcomparison<Compare> >
mypq(pqcomparison<Compare>(comp));
mypq.push(std::make_pair(1,5));
mypq.push(std::make_pair(1,6));
mypq.push(std::make_pair(2,7));
for (; !mypq.empty(); ){
std::cout << mypq.top().second << std::endl;
mypq.pop();
}
return;
}
宣言だけをコンパイルすると、std::priority_queue
エラーは発生しません。しかし、関数全体をコンパイルしようとすると、 のすべてのメンバー関数に関するエラー メッセージが表示されますstd::priority_queue
。
誰かが解決策を提案してもらえますか?