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

std::is_sorted

Defined in header <algorithm>

?

?

template< class ForwardIt > bool is_sorted( ForwardIt first, ForwardIt last );

(1)

(since C++11)

template< class ExecutionPolicy, class ForwardIt > bool is_sorted( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last );

(2)

(since C++17)

template< class ForwardIt, class Compare > bool is_sorted( ForwardIt first, ForwardIt last, Compare comp );

(3)

(since C++11)

template< class ExecutionPolicy, class ForwardIt, class Compare > bool is_sorted( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Compare comp );

(4)

(since C++17)

检查范围内的元素是否[first, last)按非降序排序。

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

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

2,4%29与%281,3%29相同,但根据policy这些过载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last

-

the 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 (i.e. is ordered before) 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 an object of type ForwardIt can be dereferenced and then implicitly converted to both of them. ?

类型要求

---。

返回值

true如果范围中的元素按升序排序。

复杂性

直线在之间的距离firstlast...

例外

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

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

可能的实施

第一版

*。

模板<class ForwardIt>布勒[医]排序%28 ForwardIt First,Forwardit Lest%29{返回STD::is[医]分门别类[医]直到%281,最后%29==最后;}

第二版

模板<类向前,类比较>bool是[医]排序%28 ForwardIt First,Forwardit Lest,比较comp%29{返回std::is[医]分门别类[医]直到%281,最后,comp%29=最后;}

二次

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <iterator>
int main() 
{
    int digits[] = {3, 1, 4, 1, 5};
 
    for (auto i : digits) std::cout << i << ' ';
    std::cout << ": is_sorted: " << std::boolalpha
              << std::is_sorted(std::begin(digits), std::end(digits)) << '\n';
 
    std::sort(std::begin(digits), std::end(digits));
 
    for (auto i : digits) std::cout << i << ' ';
    std::cout << ": is_sorted: "
              << std::is_sorted(std::begin(digits), std::end(digits)) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
3 1 4 1 5 : is_sorted: false
1 1 3 4 5 : is_sorted: true

二次

另见

is_sorted_until (C++11)

finds the largest sorted subrange (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com