3

为什么有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/lexicographical_compare_three_way
https://en.cppreference.com/w/cpp/algorithm/ranges/lexicographical_compare

4

1 回答 1

4

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

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

template<input_­range R1, input_­range R2, class Proj1 = identity,
         class Proj2 = identity,
         indirect_­strict_­weak_­order<projected<iterator_t<R1>, Proj1>,
                                    projected<iterator_t<R2>, Proj2>> Comp = ranges::less>
  constexpr bool
    lexicographical_compare(R1&& r1, R2&& r2, Comp comp = {},
                            Proj1 proj1 = {}, Proj2 proj2 = {});

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

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

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

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

于 2021-04-23T22:44:59.010 回答