【发布时间】:2023-03-07 01:51:01
【问题描述】:
我正在写一个排序算法,它带有一个比较函数,similar to std::sort:
template <class RandomIt, class Compare>
void sort(RandomIt first, RandomIt last, Compare comp);
在我看来,模板参数Compare 与Compare named requirement 完美匹配。我试图了解如何使用 C++ 20 概念指定该约束,例如 std::strict_weak_order 和 std::equivalence_relation,但我有点困惑。
如果我引用the article on cppreference,
类型
T满足 比较 ifT类型满足 BinaryPredicate 和 Given
comp,T类型的对象equiv(a, b),相当于!comp(a, b) && !comp(b, a)的表达式
std::strict_weak_ordering 可以在上面的描述中捕获我对comp 的约束,但是equiv 呢? std::equivalence_relation 将关系作为第一个模板参数。我的排序功能会是什么?
【问题讨论】:
-
有什么原因您不想使用
std::sortable函数使用的std::sortable要求吗? -
@NicolBolas Urg,谢谢。这正是我想要的。我想知道为什么他们没有在 cppreferences 上使用
std::sort的概念。他们只在std::ranges::sort中使用它。我也在查看这里的列表:en.cppreference.com/w/cpp/header/concepts。那里没有提到std::sortable。 -
std::sortable是sort的要求,而不是比较器的要求。比较器仍应满足std::strict_weak_order(或std::indirect_strict_weak_order,视情况而定)。 -
equiv(a,b)定义为与!comp(a,b) && !comp(b,a)相同,it 应满足std::equivalence_relation。
标签: c++ c++20 c++-concepts