Re: [問題] C++ 如何修改compare
: 推 aa0917954358: 可以請教一下 overloading operator()跟operator< 04/20 22:36
: → aa0917954358: 當作compare function的概念差在哪裡嗎? 04/20 22:36
operator () 是比較者 (comparator) 的成員函式
這個比較者是一個"第三方"物件, 它會向被比較的人問資料來據以比對
operator < 是物件本身的成員函式
這是這個物件自己帶有 < 的語意
那在呼叫演算法時不寫 comparator 就會使用標準預設的 std::less
std::less 做的事就是使用 < 來比較兩個給定的物件
也就是說這是把比較的邏輯交給物件本身決定
====
operator < 由於是物件本身的成員, 帶有"物件自己的性質"這個語意
因此只適合用在物件本身有個很強的排序性質的時候 (例如數字或字串)
其他的狀況 (例如資料紀錄) 本身沒有很強的排序語意時
使用第三方比較者比較合適
也因為沒有排序語意, 會很常發生排序依據會在不同次排序之間有所改變
使用比較者就很容易抽換不同的比較者達成更換排序依據的效果
--
'Oh, Harry, don't you see?' Hermione breathed. 'If she could have done
one thing to make absolutely sure that every single person in this school
will read your interview, it was banning it!'
---'Harry Potter and the order of the phoenix', P513
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.195.192.32
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1555790351.A.9E5.html
→
04/21 21:17,
5年前
, 1F
04/21 21:17, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):