std::priority_queue 包含一个包含状态漏斗的结构。

问题描述 投票:0回答:1

存在两个关于std::priority_queue的问题的优秀答案,其中包含有 结构阶层:

如果我需要这些比较结构来保持一个状态,比如一个ofstream的对象,怎么办?

先谢谢你。

c++ templates functor
1个回答
1
投票

你可以用需要状态的构造函数定义compare functor。

    struct Compare
    {
        State state;

        Compare(State state)
            : state(state)
        {
        }

        bool operator()(const Item& a, const Item& b)
        {
            ... // use state
        }
    };

然后把用所需状态构造的实例传递给priority_queue。构造者:

    priority_queue<Item, std::vector<Item>, Compare> queue(Compare(state));
© www.soinside.com 2019 - 2024. All rights reserved.