Namespaces
Variants

std::experimental::ranges:: lexicographical_compare

From cppreference.net
template < InputIterator I1, Sentinel < I1 > S1, InputIterator I2, Sentinel < I2 > S2,

class Proj1 = ranges:: identity , class Proj2 = ranges:: identity ,
class Comp = ranges:: less <> >
requires IndirectStrictWeakOrder < Comp, projected < I1, Proj1 > , projected < I2, Proj2 >>
bool lexicographical_compare ( I1 first1, S1 last1, I2 first2, S2 last2,
Comp comp = Comp { } ,

Proj1 proj1 = Proj1 { } , Proj2 proj2 = Proj2 { } ) ;
(1) (ranges TS)
template < InputRange R1, InputRange R2,

class Proj1 = ranges:: identity , class Proj2 = ranges:: identity ,
class Comp = ranges:: less <> >
requires IndirectStrictWeakOrder < Comp, projected < ranges:: iterator_t < R1 > , Proj1 > ,
projected < ranges:: iterator_t < R2 > , Proj2 >>
bool lexicographical_compare ( R1 && r1, R2 && r2, Comp comp = Comp { } ,

Proj1 proj1 = Proj1 { } , Proj2 proj2 = Proj2 { } ) ;
(2) (ranges TS)
1) 检查第一个范围 [ first1 , last1 ) 是否在字典序上 小于 第二个范围 [ first2 , last2 ) 。元素分别通过投影函数 proj1 proj2 处理后,使用给定的二元比较函数 comp 进行比较。
2) (1) 相同,但使用 r1 作为第一源范围, r2 作为第二源范围,如同使用 ranges:: begin ( r1 ) 作为 first1 ranges:: end ( r1 ) 作为 last1 ranges:: begin ( r2 ) 作为 first2 ,以及 ranges:: end ( r2 ) 作为 last2

字典序比较是一种具有以下特性的操作:

  • 两个范围按元素逐个比较。
  • 第一个不匹配的元素决定了哪个范围在字典序上更 或更
  • 如果一个范围是另一个的前缀,则较短的范围在字典序上更
  • 如果两个范围具有等效元素且长度相同,则这两个范围在字典序上 相等
  • 空范围在字典序上比任何非空范围都
  • 两个空范围在字典序上 相等

目录

参数

first1, last1 - 要检验的第一个元素范围
r1 - 要检验的第一个元素范围
first2, last2 - 要检验的第二个元素范围
r2 - 要检验的第二个元素范围
comp - 应用于投影元素的比较函数
proj1 - 应用于第一个范围元素的投影
proj2 - 应用于第二个范围元素的投影

返回值

true 如果第一个范围在字典序上 小于 第二个范围。

复杂度

最多 2·min(N1, N2) 次比较操作的应用,其中 N1 = last1 - first1 N2 = last2 - first2

可能的实现

template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2,
         class Proj1 = ranges::identity, class Proj2 = ranges::identity,
         class Comp = ranges::less<>>
    requires IndirectStrictWeakOrder<Comp, projected<I1, Proj1>, projected<I2, Proj2>>
bool lexicographical_compare(I1 first1, S1 last1, I2 first2, S2 last2,
                             Comp comp = Comp{}, 
                             Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{})
{
    for (; (first1 != last1) && (first2 != last2); (void) ++first1, (void) ++first2)
    {
        if (ranges::invoke(comp, ranges::invoke(proj1, *first1),
                                 ranges::invoke(proj2, *first2)))
            return true;
        if (ranges::invoke(comp, ranges::invoke(proj2, *first2),
                                 ranges::invoke(proj1, *first1)))
            return false;
    }
    return (first1 == last1) && (first2 != last2);
}

示例

参见

若一个范围按字典序小于另一个范围则返回 true
(函数模板)
判断两个元素集合是否相同
(函数模板)