0

ここから取得http://www.cplusplus.com/reference/queue/priority_queue/

Compare
A binary predicate that takes two elements (of type T) as arguments and returns a bool.
The expression comp(a,b), where comp is an object of this type and a and b are elements in the container, shall return true if a is considered to go before b in the strict weak ordering the function defines.

比較関数を書くとき、どの要素がキューで長く待っているかを知る方法はありますか?

新しい要素が挿入されるたびに比較関数が呼び出されると仮定すると、「a」は常に新しいアイテムになり、「b」はすでにキューにある要素になりますか? それとも違う働きをしますか?

私の考えは、次のようなものでした:

bool my_class::operator()(const my_class &a, const my_class &b) {
    //Lower priority comes first
    return (a.get_priority() < b.get_priority());
}

'a' と 'b' の優先順位が同じ場合、'b' が優先順位を与えられます。

std::queue がどのように機能するか、および目標を達成する方法についてフィードバックをお寄せいただきありがとうございます。

4

2 に答える 2

0

オブジェクトのタイムスタンプを保持し、オブジェクトがキューに挿入されたときに設定します。次に、そのタイムスタンプを比較の条件の一部として使用します。

于 2013-09-16T08:30:53.013 に答える