你的問題有點不清楚。我假設你想要分類的結構是Toast
。
如果你只是想根據bool
Toast
s到優先,說status
,你的比較對象是很容易的:
class mycomparison
{
public:
bool operator() (const Toast& lhs, const Toast& rhs) const
{
return lhs.status < rhs.status;
}
};
現在我要承擔以下就此展開,給出Toast
有以下bool
成員最顯著的順序排列,至少顯著:vip
,notvip
,VIP1 ,
的Vip3 , and
notVip2`,我還假設在比較「不」的成員應該被反轉:
class mycomparison
{
public:
bool operator() (const Toast& lhs, const Toast& rhs) const
{
return lhs.vip < rhs.vip || lhs.vip == rhs.vip && // return true if rhs.vip is the only true or continue to test
(lhs.noVip > rhs.noVip || lhs.noVip == rhs.noVip && // return true if rhs.noVip is the only false or continue to test
(lhs.Vip1 < rhs.Vip1 || lhs.Vip1 == rhs.Vip1 && // return true if lhs.Vip1 is less than rhs.Vip1 or continue to test
(lhs.Vip3 < rhs.Vip3 || lhs.Vip3 == rhs.Vip3 && // return true if lhs.Vip3 is less than rhs.Vip3 or continue to test
lhs.notVip2 > rhs.notVip2))); // return true if lhs.notVip2 is greater than rhs.notVip2 or return false
}
};
注意,如果構件是bool
S或operator<
,operator>
和operator==
被定義爲其中任何其它類型的任一這些mycomparison
類的工作正常。要在std::priority_queue
中使用mycomparision
,您只需要將其作爲std::priority_queue
的比較對象傳遞給它,例如:std::priority_queue<Toast> foo(mycomparison());
我建議您閱讀例如[這個'std :: priority_queue'引用](http://en.cppreference.com/w/cpp/container/priority_queue),它包含一個鏈接,指向比較函數需要什麼的確切定義。 – 2014-10-05 11:02:42
你有兩個代表彼此完全相反的屬性嗎?如果是這樣,那麼首先將其減少到一個屬性。然後,使用std :: less來比較該屬性,即將包含對象的比較委託給其中的根據字段的比較。 –
2014-10-05 11:04:53
請澄清你的問題。這個貴賓僵硬沒有任何意義。 – juanchopanza 2014-10-05 11:36:49