cpp 中内置/预定义的比较器

发布于 2025-01-15 14:19:27 字数 126 浏览 4 评论 0原文

最近从STL学习了cpp中的比较器。

我开始知道我们可以使用 greater<>() 作为排序的第三个参数,而不是编写自己的逻辑。

只是想知道 cpp 中有多少内置比较器。

Recently I learnt about comparators in cpp from STL.

I came to know we can use greater<>() as third argument for sorting instead of writing own logic.

Just curious to know how many inbuilt comparators are there in cpp.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被翻牌 2025-01-22 14:19:27

标准库定义了您所期望的与内置运算符类似的内容:

std::equal_to      // ==
std::not_equal_to  // !=
std::less          // <
std::less_equal    // <=
std::greater       // >
std::greater_equal // >=

由于 C++20 还限制了 std::ranges 命名空间中所有这些比较函数对象的版本以及std::compare_third_way,它类似于内置三向比较运算符<=>

有关这些函数对象的参考,请参阅 https://en.cppreference.com/ w/cpp/utility/function#Comparisons

The standard library defines pretty much what you would expect as analogues to the built-in operators:

std::equal_to      // ==
std::not_equal_to  // !=
std::less          // <
std::less_equal    // <=
std::greater       // >
std::greater_equal // >=

Since C++20 also constrained versions of all these comparison function objects in the std::ranges namespace as well as std::compare_­three_­way, which is the analogue to the built-in three-way comparison operator <=>.

For a reference of these function objects see https://en.cppreference.com/w/cpp/utility/functional#Comparisons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文