简体   繁体   English

如果std :: greater <>,那么为什么std :: less(而不是std :: lesser <>)?

[英]If std::greater<>, then why std::less (and not std::lesser<>)?

This apparently looks like a grammatical (and funny?) question but I hope it is not. 这显然看起来像一个语法(和有趣?)的问题,但我希望它不是。

I seriously wonder why we have std::greater<> if we don't have std::lesser<> (instead we have std::less<> )? 如果我们没有std::lesser<> (我们有std :: less <> ),我真的很想知道为什么我们有std :: greater <> Doesn't it make sense to have either greater and lesser or great and less ? 难道不是很有意义有任何greaterlessergreatless I ask this question because I pretty much mess it up every single time and need to Google it. 我问这个问题是因为我每次都搞乱它并且需要谷歌它。

Is there some naming convention that the standard follows? 是否有一些标准遵循的命名约定?

I would say, and this is really just informed speculation, that the authors of the standard made an explicit choice to go one way rather than the other. 我会说,这实际上只是一个明智的猜测,标准的作者明确选择了一种方式而不是另一种方式。 English, in its near-infinite capacity for confusing people, has many ways for expressing the same idea 英语在几乎无限的容易混淆的人的能力,有很多方式表达相同的想法

a is GREATER than b   =>   a is the GREATER value
a is LESS    than b   =>   a is the LESSER  value

However, since the intent of std::greater and std::less is to return a Boolean value indicating whether the related condition is true, the left-hand column above is the right way to go. 但是,由于std::greaterstd::less意图是返回一个布尔值,指示相关条件是否为真,因此上面的左侧列是正确的方法。 If instead it returned the greater or lesser value, it would be more apt to use the right hand column. 如果它返回了更大或更小的值,则更容易使用右侧列。

In fact, if you look at the ISO standard ( C++11 20.8.5 Comparisons /4 and /5 ), you'll see how they're defined (slightly reformatted): 事实上,如果你看一下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.

Those comment sections would clearly be read as "returns x is greater than y" and "returns x is less than y". 这些评论部分显然将被理解为“返回x 大于 y”和“返回x 小于 y”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM