0

を使用して記述された関数がありますが、std::multimapこれは正確にstd::multimap. 分析の結果、 をheapstd::multimapとしてのみ使用していることに気付きました。そのため、この使用法がより高速になることを期待して、ヒープ操作のみを許可する に置き換えようとしています。std::priority_queue

もちろん、 の要素の型は でstd::priority_queueある必要があり、その後、ペアの最初の値 ( の実際のキー) のみをstd::pair<mmKey, mmValue>比較するカスタム コンパレータを に渡します。std::priority_queuestd::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

誰かが解決策を提案してもらえますか?

4

1 に答える 1

4

最も厄介な解析ケースがかなりうまくマスクされています。この行を変更するだけです std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int> >, pqcomparison<Compare> > mypq(pqcomparison<Compare>(comp));

に: std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int> >, pqcomparison<Compare> > mypq{pqcomparison<Compare>(comp)};

()で変更されていることに注意してください{}。C++11 を使用していない場合は、括弧を追加する必要があります。mypq((pqcomparison<Compare>(comp)))

于 2013-09-20T10:07:46.047 に答える