C++11标准模板(STL)- 算法(std::nth_element)
定义于头文件 <algorithm>
算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last)
,其中 last
指代要查询或修改的最后元素的后一个元素。
将给定的范围部分排序,确保其按给定元素划分
std::nth_element
template< class RandomIt > |
(1) | (C++20 前) |
template< class RandomIt > |
(C++20 起) | |
template< class ExecutionPolicy, class RandomIt >
void nth_element( ExecutionPolicy&& policy, RandomIt first, RandomIt nth, RandomIt last ); |
(2) | (C++17 起) |
template< class RandomIt, class Compare >
void nth_element( RandomIt first, RandomIt nth, RandomIt last, Compare comp ); |
(3) | (C++20 前) |
template< class RandomIt, class Compare >
constexpr void nth_element( RandomIt first, RandomIt nth, RandomIt last, Compare comp ); |
(C++20 起) | |
template< class ExecutionPolicy, class RandomIt, class Compare >
void nth_element( ExecutionPolicy&& policy, Compare comp ); |
(4) | (C++17 起) |
nth_element
是部分排序算法,它重排 [first, last)
中元素,使得:
nth
所指向的元素被更改为假如[first, last)
已排序则该位置会出现的元素。- 这个新的
nth
元素前的所有元素小于或等于新的nth
元素后的所有元素。
更正式而言, nth_element
以升序部分排序范围 [first, last)
,使得对于任何范围 [first, nth)
中的 i
和任何范围 [nth, last)
中的 j
,都满足条件 !(*j < *i) (对于 (1-2) ,对 (3-4) 则为 comp(*j, *i) == false )。置于 nth
位置的元素则准确地是假如完全排序范围则应出现于此位置的元素。
nth
可以是尾迭代器,此情况下函数无效果。
1) 用 operator<
比较元素。
3) 用给定的二元比较函数 comp
比较元素。
2,4) 同 (1,3) ,但按照 policy
执行。这些重载不参与重载决议,除非 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true 。
参数
first, last | - | 定义待排序范围的随机访问迭代器 |
nth | - | 定义排序划分点的随机访问迭代器 |
policy | - | 所用的执行策略。细节见执行策略。 |
comp | - |
比较函数对象(即满足比较 (Compare) 概念的对象),若第一参数小于(即先序于)第二参数则返回 true 。
比较函数的签名应等价于如下: bool cmp(const Type1 &a, const Type2 &b); 虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 |
类型要求 | ||
- RandomIt 必须满足值可交换 (ValueSwappable) 和 遗留随机访问迭代器 (LegacyRandomAccessIterator) 的要求。
|
||
- 解引用 RandomIt 结果的类型必须满足可移动赋值 (MoveAssignable) 和可移动构造 (MoveConstructible) 的要求。
|
返回值
(无)
复杂度
1,3) 平均与 std::distance(first, last) 成线性。
2,4) O(N) 次应用谓词和 O(N log N) 次交换,其中 N = last - first 。
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 若作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
为标准策略之一,则调用 std::terminate 。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 若算法无法分配内存,则抛出 std::bad_alloc 。
注意
典型地使用内省选择算法,尽管允许其他拥有适合平均情况复杂度的选择算法。
调用示例
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <iterator>
#include <time.h>using namespace std;struct Cell
{int x;int y;Cell &operator +=(const Cell &cell){x += cell.x;y += cell.y;return *this;}bool operator <(const Cell &cell) const{if (x == cell.x){return y < cell.y;}else{return x < cell.x;}}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}int main()
{srand((unsigned)time(NULL));;std::cout.setf(std::ios_base::boolalpha);auto func1 = [](){int n = std::rand() % 10 + 100;Cell cell{n, n};return cell;};vector<Cell> cells(8);std::generate(cells.begin(), cells.end(), func1);std::cout << "cells : ";std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << "is_sorted: " << std::is_sorted(cells.begin(), cells.end());std::cout << std::endl << std::endl;std::nth_element(cells.begin(), cells.begin() + 3, cells.end());std::cout << "nth_element : ";std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << "is_sorted: " << std::is_sorted(cells.begin(), cells.end());std::cout << std::endl << std::endl;auto is_sortf = [](const Cell & a, const Cell & b){if (a.x == b.x){return a.y > b.y;}return a.x > b.x;};std::generate(cells.begin(), cells.end(), func1);std::cout << "cells : ";std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << "is_sorted: " << std::is_sorted(cells.begin(), cells.end(), is_sortf);std::cout << std::endl << std::endl;std::nth_element(cells.begin(), cells.begin() + 3, cells.end(), is_sortf);std::cout << "nth_element : ";std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << "is_sorted: " << std::is_sorted(cells.begin(), cells.end(), is_sortf);std::cout << std::endl << std::endl;return 0;
}
输出
C++11标准模板(STL)- 算法(std::nth_element)相关推荐
- 【跟学C++】C++STL标准模板库——算法详细整理(下)(Study18)
文章目录 1.简介 2.STL算法分类及常用函数 2.2.变序算法(二) 2.2.1 替换算法(2个) 2.2.2 排序算法(6个) 2.2.3 分区算法(4个) 2.2.4 可用于排序容器的算法(3 ...
- 【跟学C++】C++STL标准模板库——算法详细整理(中)(Study18)
文章目录 1.简介 2.STL算法分类及常用函数 2.1.变序算法(一) 2.2.1 初始化算法(2个) 2.2.2 修改算法(2个) 2.2.3 复制算法(6个) 2.2.4 删除算法(6个) 3. ...
- c++ 11标准模板(STL) std::vector (十一)
定义于头文件 <vector> template< class T, class Allocator = std::allocator<T> > class ...
- 【跟学C++】C++STL标准模板库——算法详细整理(上)(Study18)
文章目录 1.STL简介 2.STL算法分类及常用函数 2.1.非变序算法 2.1.1 计数算法(2个) 2.1.2 搜索算法(7个) 2.1.3 比较算法(2个) 3.总结 =========== ...
- C++11后的STL算法
文章目录 一.函数对象 二.预定义的函数对象 三.算法函数 1.自己实现foreach算法 2.自己实现的findif算法 3.自己实现bsort算法 一.函数对象 STL提供了很多处理容器的函数模板 ...
- C++11标准模板(STL)- 算法(std::set_symmetric_difference)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
- C++11标准模板(STL)- 算法 - 集合操作(在已排序范围上)(std::set_difference)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
- C++11标准模板(STL)- 算法(std::adjacent_difference)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
- C++11标准模板(STL)- 算法 - 数值运算 (std::accumulate)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
最新文章
- Spark基础知识解答
- Metro中文件夹和文件的创建
- 智慧解析第12集:老板心理学
- slim android7 nexus7,【畅玩7.0】加一直升pure nexus 7.0系统简单教程(1106更新)
- jsp页面提示“Multiple annotations found at this line: - The superclass javax.servlet.http.HttpServlet w
- react native中有关日期的组件DatePicker 示例
- App设计灵感之十二组精美的旅行App设计案例
- 添加CSS的四种方式
- java 指代对象_java-This的理解
- 源码分析:Java对象的内存分配
- python selenium 进入新标签页_python 爬虫之selenium可视化爬虫
- UI实用素材| 工作管理、日程日历 专辑,总有一款符合设计师风格
- JavaSE知识点:finalize,treeMap
- DeepMatch交友机器人:原来姹紫嫣红开遍,缘来就是你
- 如何让“后浪”热爱工作,来自“前浪”的十大拷问
- 富文本编辑器tinymce支持从word复制粘贴保留格式和图片的插件wordpaster
- 团体程序设计天梯赛-练习集-L1-031. 到底是不是太胖了
- survival | 生存分析(1):生存曲线(上)
- GitHub基本操作
- xmanager连接linux7桌面,使用Xmanager连接CentOS 7远程桌面
热门文章
- via自定义搜索引擎代码_VIA浏览器自定义logo设置
- 如何在 Windows 右键菜单中新建自己想要的文件格式
- const*与*const的区别
- iOS wifi 路由器 连接
- json.stringify()函数
- 生物信息学基础知识Day2
- asp毕业设计——基于asp+access的订单管理系统设计与实现(毕业论文+程序源码)——订单管理系统
- 数据分析编程检验——车流量统计(不能使用pandas和numpy)
- android获取应用权限管理,如何在android中获取应用程序权限设置?
- Statspack ORA-00001 unique constraint violated错误的解决