首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::lexicographical_compare

Defined in header <algorithm>

?

?

template< class InputIt1, class InputIt2 > bool lexicographical_compare( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 );

(1)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > bool lexicographical_compare( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2 );

(2)

(since C++17)

template< class InputIt1, class InputIt2, class Compare > bool lexicographical_compare( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp );

(3)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class Compare > bool lexicographical_compare( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, Compare comp );

(4)

(since C++17)

检查第一个范围[first1, last1)是字典学再少点比第二个范围[first2, last2)...

1%29个元素的比较operator<...

使用给定的二进制比较函数对3%29个元素进行比较。comp...

2,4%29与%281,3%29相同,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

词汇表比较是一种具有下列属性的操作:

  • 两个范围逐个元素进行比较。
  • 第一个不匹配元素定义了哪个范围是按字典顺序排列的。再少点更大比另一个。
  • 如果一个区域是另一个区域的前缀,则较短的范围按字典顺序排列。再少点比另一个。
  • 如果两个范围具有等效元素,且长度相同,则范围按字典顺序排列。平等...
  • 空范围是按字典顺序排列的。再少点比任何非空范围都要好。
  • 两个空范围按字典顺序排列。平等...

参数

first1, last1

-

the first range of elements to examine

first2, last2

-

the second range of elements to examine

policy

-

the execution policy to use. See execution policy for details.

comp

-

comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ?true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function object must not modify the objects passed to it. The types Type1 and Type2 must be such that objects of types InputIt1 and InputIt2 can be dereferenced and then implicitly converted to both Type1 and Type2. ?

类型要求

-InputIt 1,InputIt 2必须符合InputIterator的要求。

-前进1,前进2必须符合先行者的要求。

返回值

true如果第一个范围按字典顺序排列再少点而不是第二个。

复杂性

最多2.min%28N1,N2%29的比较运算,其中N1 =std::distance(first1, last1)N2 =std::distance(first2, last2)...

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<类InputIt 1,类InputIt 2>bool词汇学[医]比较%28InputIt1first 1、InputIt1last1、InputIt2first 2、InputIt2 2%29{for%28;%28first 1%21=last1%29&&mdash;%28first 2%21=last2%29;first 1++、%28void%29first 2++%29{if%28%2A头1<%2A前2%29返回true;如果为%28%2A头2<%2A前1%29返回false;}返回%28first 1==LASST 1%29&&%28first 2%21=LASST 2%29;}

第二版

模板<类InputIt 1,类InputIt 2,类比较>bool词典编纂[医]比较%28InputIt1first 1、InputIt1last1、InputIt2first 2、InputIt2last2,比较comp%29{for%28;%28first 1%21=last1%29&&%28first 2%21=last2%29;prep 1++,%28void%29first 2++%29{if%28comp%28;%28comp%28%2A首先,%2A前2%29%29返回true;if%28 comp%28%2A第二,%2A前1%29%29返回false;}返回%28first 1==last1%29&&p28 2%21=last2%29;}

二次

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
 
int main()
{
    std::vector<char> v1 {'a', 'b', 'c', 'd'};
    std::vector<char> v2 {'a', 'b', 'c', 'd'};
 
    std::srand(std::time(0));
    while (!std::lexicographical_compare(v1.begin(), v1.end(),
                                         v2.begin(), v2.end())) {
        for (auto c : v1) std::cout << c << ' ';
        std::cout << ">= ";
        for (auto c : v2) std::cout << c << ' ';
        std::cout << '\n';
 
        std::random_shuffle(v1.begin(), v1.end());
        std::random_shuffle(v2.begin(), v2.end());
    }
 
    for (auto c : v1) std::cout << c << ' ';
    std::cout << "< ";
    for (auto c : v2) std::cout << c << ' ';
    std::cout << '\n';
}

二次

可能的产出:

二次

代码语言:javascript
复制
a b c d >= a b c d 
d a b c >= c b d a 
b d a c >= a d c b 
a c d b < c d a b

二次

另见

equal

determines if two sets of elements are the same (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com