如果 std::greater<>,那么为什么是 std::less(而不是 std::lesser<>)?
If std::greater<>, then why std::less (and not std::lesser<>)?
这显然看起来像是一个语法(有趣?)问题,但我希望不是。
我很想知道为什么我们没有std :: greem&lt;>如果我们没有 std::lesser<>
(而是我们有std :: Less&lt;>)?拥有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"answers"返回x是 sill 比y"。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 是std :: set&lt; std :: future&gt;不可能存在
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- 使用 std::vector<boost::shared_ptr<Base_Class>> 或 boost::p tr_vector 的性能注意事项是什么<Base>
- std::map<std::set, double> AND std:<long>map< std::p air<long, long>, double>
- 如何获取std::vector<DMatch>