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

std::nth_element

Defined in header <algorithm>

?

?

template< class RandomIt > void nth_element( RandomIt first, RandomIt nth, RandomIt last );

(1)

?

template< class ExecutionPolicy, class RandomIt > void nth_element( ExecutionPolicy&& policy, RandomIt first, RandomIt nth, RandomIt last );

(2)

(since C++17)

template< class RandomIt, class Compare > void nth_element( RandomIt first, RandomIt nth, RandomIt last, Compare comp );

(3)

?

template< class ExecutionPolicy, class RandomIt, class Compare > void nth_element( ExecutionPolicy&& policy, RandomIt first, RandomIt nth, RandomIt last, Compare comp );

(4)

(since C++17)

nth_element中的元素重新排列的部分排序算法。[first, last)使:

  • 指的元素nth被更改为在该位置上发生的任何元素,如果[first, last)被分类了。
  • 在这个新的nth元素后面的元素小于或等于nth元素。

更正式一点,nth_element部分排序范围[first, last)在升序中使条件!(*j < *i)%281-2%29,或comp(*j, *i) == false%283-4%29%29i在范围内[first, nth)对于任何j在范围内[nth, last).放置在nth如果对范围进行了完全排序,则位置正是此位置中将出现的元素。

nth可能是结束迭代器,在这种情况下,函数没有任何效果。

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

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

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

参数

first, last

-

random access iterators defining the range sort

nth

-

random access iterator defining the sort partition point

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 RandomIt can be dereferenced and then implicitly converted to both of them. ?

类型要求

---。

-取消引用的随机数的类型必须符合可移动分配和移动可建的要求。

返回值

%280%29

复杂性

1,3%29线性std::distance(first, last)平均来说。

2.4%29 O%28N%29的谓词应用程序,以及O%28 N日志N%29交换,其中N=最后一位。

例外

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

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

注记

所使用的算法通常是自选虽然其他选择算法在适当的平均情况下,复杂度是允许的。

二次

代码语言:javascript
复制
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
 
int main()
{
    std::vector<int> v{5, 6, 4, 3, 2, 6, 7, 9, 3};
 
    std::nth_element(v.begin(), v.begin() + v.size()/2, v.end());
    std::cout << "The median is " << v[v.size()/2] << '\n';
 
    std::nth_element(v.begin(), v.begin()+1, v.end(), std::greater<int>());
    std::cout << "The second largest element is " << v[1] << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
The median is 5
The second largest element is 7

二次

另见

partial_sort_copy

copies and partially sorts a range of elements (function template)

stable_sort

sorts a range of elements while preserving order between equal elements (function template)

sort

sorts a range into ascending order (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com