[英]If std::greater<>, then why std::less (and not std::lesser<>)?
這顯然看起來像一個語法(和有趣?)的問題,但我希望它不是。
如果我們沒有std::lesser<>
(我們有std :: less <> ),我真的很想知道為什么我們有std :: greater <> ? 難道不是很有意義有任何greater
和lesser
或great
和less
? 我問這個問題是因為我每次都搞亂它並且需要谷歌它。
是否有一些標准遵循的命名約定?
我會說,這實際上只是一個明智的猜測,標准的作者明確選擇了一種方式而不是另一種方式。 英語在幾乎無限的容易混淆的人的能力,有很多方式表達相同的想法
a is GREATER than b => a is the GREATER value
a is LESS than b => a is the LESSER value
但是,由於std::greater
和std::less
的意圖是返回一個布爾值,指示相關條件是否為真,因此上面的左側列是正確的方法。 如果它返回了更大或更小的值,則更容易使用右側列。
事實上,如果你看一下ISO標准( C++11 20.8.5 Comparisons /4 and /5
),你會看到它們是如何定義的(稍微重新格式化):
template <class T> struct greater {
bool operator()(const T& x, const T& y) const;
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
}; // operator() returns x > y.
template <class T> struct less {
bool operator()(const T& x, const T& y) const;
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
}; // operator() returns x < y.
這些評論部分顯然將被理解為“返回x 大於 y”和“返回x 小於 y”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.