在线文档教程
C++
算法 | Algorithm

std::is_sorted_until

STD::是[医]分门别类[医]直到

Defined in header
template< class ForwardIt > ForwardIt is_sorted_until( ForwardIt first, ForwardIt last (1)(since C++11)
template< class ExecutionPolicy, class ForwardIt > ForwardIt is_sorted_until( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last (2)(since C++17)
template< class ForwardIt, class Compare > ForwardIt is_sorted_until( ForwardIt first, ForwardIt last, Compare comp (3)(since C++11)
template< class ExecutionPolicy, class ForwardIt, class Compare > ForwardIt is_sorted_until( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Compare comp (4)(since C++17)

检查范围[first, last)并发现最大的范围开始于first其中元素按升序排序。

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. ​

类型要求

---。

返回值

最大范围的上限,起始于first其中元素按升序排序。也就是说,最后一个迭代器it哪个范围[first, it)被分类了。

复杂性

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

例外

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

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

  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<class ForwardIt>向前是[医]分门别类[医]直到%28 Forwardit First,Forwardit持续%29{if%281%21=最后%29{Forwardit Next=First;而%28+Next%21=Late%29(if%28)%2A下一个<%2A首先%29返回下一步;First=Next;}最后返回;}

第二版

模板<类向前,类比较>向前[医]分门别类[医]直到%28 ForwardIt First,Forwardit Lest,比较comp%29(使用名称空间std::占位符;Forwardit=std:::邻)[医]找到%28First,Lest,std::binding%28comp,[医]2,[医]1%29%29;返回它==最后?最后:std::下一步%28 it%29;}

二次

#include <iostream> #include <algorithm> #include <iterator> #include <random> int main() { std::random_device rd; std::mt19937 g(rd() const int N = 6; int nums[N] = {3, 1, 4, 1, 5, 9}; const int min_sorted_size = 4; int sorted_size = 0; do { std::shuffle(nums, nums + N, g int *sorted_end = std::is_sorted_until(nums, nums + N sorted_size = std::distance(nums, sorted_end for (auto i : nums) std::cout << i << ' '; std::cout << " : " << sorted_size << " initial sorted elements\n"; } while (sorted_size < min_sorted_size }

二次

可能的产出:

二次

4 1 9 5 1 3 : 1 initial sorted elements 4 5 9 3 1 1 : 3 initial sorted elements 9 3 1 4 5 1 : 1 initial sorted elements 1 3 5 4 1 9 : 3 initial sorted elements 5 9 1 1 3 4 : 2 initial sorted elements 4 9 1 5 1 3 : 2 initial sorted elements 1 1 4 9 5 3 : 4 initial sorted elements

二次

另见

is_sorted (C++11)checks whether a range is sorted into ascending order (function template)

© cppreference.com

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

http://en.cppreference.com/w/cpp/Algorithm/is[医]分门别类[医]直到