std::lexicographyal_compare_third_way 用于范围

She*_*rei 5 c++ c++20 std-ranges

为什么有std::lexicographical_compare_three_way,却没有std::ranges::lexicographical_compare_three_way

中有参数 Comp std::ranges::lexicographical_compare,但它相当无用,因为bool当需要比较类别类型之一时,函数返回 。

以下是 cppref 的一些链接
https://en.cppreference.com/w/cpp/algorithm/lexicographyal_compare_ Three_way
https://en.cppreference.com/w/cpp/algorithm/ranges/lexicographyal_compare

Bar*_*rry 6

\n

为什么有std::lexicographical_compare_three_way,却没有std::ranges::lexicographical_compare_three_way

\n
\n

中的算法std::ranges受 s 约束concept,而 中的算法则std不受 s 约束。因此,例如,std::ranges::lexicographical_compare(使用双向比较,默认为<)指定为:

\n
template<input_\xc2\xadrange R1, input_\xc2\xadrange R2, class Proj1 = identity,\n         class Proj2 = identity,\n         indirect_\xc2\xadstrict_\xc2\xadweak_\xc2\xadorder<projected<iterator_t<R1>, Proj1>,\n                                    projected<iterator_t<R2>, Proj2>> Comp = ranges::less>\n  constexpr bool\n    lexicographical_compare(R1&& r1, R2&& r2, Comp comp = {},\n                            Proj1 proj1 = {}, Proj2 proj2 = {});\n
Run Code Online (Sandbox Code Playgroud)\n

值得注意的是,我们有一个概念indirect_strict_weak_order来解释比较操作的语法和语义要求,以便使该算法有意义。

\n

然而,还没有人完成制定相应的concepts 以进行三向比较的工作。请注意,它strict_weak_order具有非常强的语义要求,我们需要一个假设的等效名称strict_weak_order_three_way(或其他一些不那么糟糕的名称)。

\n

这里有趣的是,我们不需要拒绝partial_ordering而只要求域中没有两个元素比较为partial_ordering::unordered

\n

一旦我们有了这样的concept东西,那么指定和实现std::ranges::lexicographical_compare_three_way就非常简单了。

\n