C ++中的std :: nth_element()

2023-11-07

The standard library of C++ has a huge number of functions that are not so explored but can be very handy in case of specific usages. It helps you to write less number of codes and do things quickly. Say, you are building some backend system in C++ and that has thousands of lines already. In such scenarios, these standard library functions help a lot to reduce codebase size. Also in competitive coding where submission time matters, these kinds of function usages may make your day!

C ++的标准库具有大量功能,虽然不那么探讨,但是在特定用法的情况下非常方便。 它可以帮助您编写更少的代码并快速执行操作。 假设您正在用C ++构建一些后端系统,并且已经有成千上万行。 在这种情况下,这些标准库函数可以极大地减少代码库的大小。 同样在提交时间很重要的竞争性编码中,这些功能的使用可能会让您度过一整天!

nth_element() is one such std function which helps to find nth element out of a list range if the list was sorted.

nth_element()是一个这样的std函数,如果对列表进行了排序,则它有助于在列表范围之外找到第n个元素。

For example,

例如,

Say the list is:
[4, 1, 2, 3, 6, 7, 5]

Using nth_element() function if you want to find 3rd element(0-indexed) out of the entire range you may have your list updated to something like,

如果您想在整个范围之外找到第三个元素(索引为0),请使用nth_element()函数,将列表更新为类似的内容,

[3, 1, 2, 4, 6, 7, 5]

Which tells you that the 3rd element in the sorted list would be arr[3]=4

告诉您排序列表中的第三个元素将是arr [3] = 4

The most important feature of this function is:

此功能的最重要功能是:

  1. It only gives your nth element in the correct order

    它只会以正确的顺序给出您的第n个元素

  2. For the rest of the element, you can't guess what would be the arrangement. It depends on the compiler. What you can get assured is that the elements preceding the nth element would be all smaller than the nth element and the element which comes after the nth element are greater than the nth element.

    对于元素的其余部分,您无法猜测会是什么安排。 这取决于编译器。 您可以放心,第n个元素之前的元素都小于第n个元素,而第n个元素之后的元素大于第n个元素。

Syntax of n-th element:

第n个元素的语法:

void nth_element(iterator start, iterator nth, iterator end)
// so it doesn't returns anything,
// rather updates the list internally

iterator start  = start of your range
iterator end    = end of your range
iterator nth    = nth term you want to see in position 
                  if the list was sorted (0-indexed)

So for the above example say the vector name is arr. Then, it would be:

因此,对于上面的示例,向量名称为arr 。 然后,将是:

nth_iterator(arr.begin(),arr+3,arr.end())

Since the range is first to last of the list and we need to find the 3rd element(0-indexed) if the list was sorted.

由于范围是列表的第一个到最后一个,如果列表已排序,我们需要找到第三个元素(索引为0)。

Example:

例:

#include <bits/stdc++.h>
using namespace std;

//to print the vector
void print(vector<int> arr)
{
    for (auto it : arr) {
        cout << it << " ";
    }
    cout << endl;
}

int main()
{
    //to see how it's initialized 
    vector<int> arr{ 4, 1, 2, 3, 6, 7, 5 };

    cout << "Printing initially...\n";
    print(arr);

    //find 3rd element if list was sorted
    nth_element(arr.begin(), arr.begin() + 3, arr.end());
    
    cout << "the 3rd element if the list was sorted is: " << arr[3] << endl;

    cout << "the new rearrangement of the array...\n";
    print(arr);
    
    return 0;
}

Output:

输出:

Printing initially...
4 1 2 3 6 7 5
the 3rd element if the list was sorted is: 4
the new rearrangement of the array...
3 1 2 4 5 6 7

Application or Usages:

应用或用途:

We can use this standard library function whenever we need to find the nth_element() if the array was sorted at one go.

如果数组是一次性排序的,则每当需要查找nth_element()时,便可以使用此标准库函数。

An important application can be finding the median in an unsorted array.

一个重要的应用可能是在未排序的数组中找到中值

翻译自: https://www.includehelp.com/stl/std-nth-element.aspx

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C ++中的std :: nth_element() 的相关文章

随机推荐

  • Spring Boot中单元测试数据库的切换策略

    问题缘起 单元测试默认情况下使用嵌入式数据库 例如H2 如果要切换为MySQL 直接移除H2驱动 在application properties yml 配置相应的连接信息 都不起作用 那该如何切换配置呢 单元测试数据库 在SpringBo
  • 如何给Python写注释

    给程序写注释是一个很好的习惯 提高程序的可读性 写注释是不可少的步骤 Python与其他语言一样提供了两种注释方法 单行注释和多行注释 单行注释 Python中使用 进行单行注释 这里是一个单行注释 print 翔宇亭IT乐园 www bi
  • ap忘记管理ip地址怎么办_路由器刷集客固件,低成本实现AC+AP组网

    某讯K2T路由器刷集客固件 可以作为无线AP使用 多个K2T实现无缝漫游功能 通过微AC或者ESXI安装集客AC可以实现对AP进行管理 低成本的实现AC AP组网 确定版本号140版本 158以上版本的需要拆机 操作步骤 第一步 开启tel
  • 2019中国城市排名出炉——2019新一线城市有没有你的家乡!?

    新一线城市研究所在上海发布 2019城市商业魅力排行榜 新一线城市研究所收集了170个主流消费品牌的商业门店数据 18家各领域头部互联网公司的用户行为数据和数据机构的城市大数据 对337个中国地级及以上城市进行了评估 排行榜沿用了上一年的五
  • java集合框架

    这图画的真洒脱 光一个stack就有很多可探索了
  • 那些年踩过的坑——服务器中文路径

    从11年学编程至今已有十个年头 其实有时候也很后悔选择这个专业 整日和电脑相偎相依 人的思维和沟通能力也趋向机器 和别人聊天也不知道怎么开口 算法的一个评定标准就是以最少的语句实现所需的功能 但和别人聊天则不能这样 太直接简单会让人变得无趣
  • 总结了9款Mac端超好用的免费开源软件,你还有更好的推荐吗?

    与Windows相比 Mac上的软件 不仅不稀缺 并且大多数都更加精致 还没有乱七八糟烦人的弹窗骚扰 所以 本期就为大家盘点盘点Mac上有超好用的免费开源神器 1 Tincta 官网 https codingfriends github i
  • glibc堆内存管理

    glibc堆内存管理 背景 应用出现SIGABRT crash 报错信息为 malloc invalid size unsorted 即是在应用调用malloc分配内存时出现异常导致的crash 管理结构 进程虚拟地址空间被划分为代码段 数
  • eclipse下JNI的初步实现

    eclipse下JNI的初步实现 JNI java native interface 为java应用程序提供调用本地方法的接口 The standard Java class library may not support the plat
  • build打包后怎么查看源码 vue_vue2源码解析一:打包与构建流程

    本系列文章 基于vue 2 6 11进行解析 不追究每行代码分析清楚 但求把握大体的重点 比如源码构建流程 如何实现数据双向绑定 如何解析模板 如何解析一个组件的data method computed等属性 如何实现在weex web等多
  • LeetCode-1326. Minimum Number of Taps to Open to Water a Garden

    There is a one dimensional garden on the x axis The garden starts at the point 0 and ends at the point n i e The length
  • 深圳大学软件工程MOOC章节测试答案

    第一章 章节测试 一 单选题 共 90 00 分 1 下列 不是软件工程方法学中的要素 A 方法 B 工具 C 程序 D 过程 满分 10 00 分 得分 10 00 分 你的答案 C 教师评语 暂无 2 软件工程方法学的目的是 使软件生产
  • RK3588s imx415相机适配及ISP调优系列(三)--- RKISP调试环境配置

    经过上篇的相机配置后 两个mipi相机已经可以正常出图了 其实对于rk系列如何配置mipi相机 网上已有不少相关资料均可参考借鉴 RK3588s imx415相机适配及ISP调优系列 一 RK3588s imx415相机适配及ISP调优系列
  • Fast DDS入门二、Fast DDS在Windows平台的编译安装

    Fast DDS入门五 在Windows平台创建一个简单的Fast DDS示例程序 1 Fast DDS动态库的编译安装 本节提供了在Windows环境中从源代码安装Fast DDS的说明 将安装以下软件包 foonathan memory
  • Storcli工具linux命令

    storcli命令使用 设置其他盘的JBOD模式 使用storcli64工具进行查看RAID状态 storcli64 c0 show 使用storcli64工具进行删除RAID storcli64 c0 vall del force 例如
  • 具有最大和的连续子数组(动态规划法)

    题目 给定一个整数数组 nums 找到一个具有最大和的连续子数组 子数组最少包含一个元素 返回其最大和 示例 1 输入 nums 2 1 3 4 1 2 1 5 4 输出 6 解释 连续子数组 4 1 2 1 的和最大 为 6 思路 动态规
  • apollo 轨迹预测介绍

    转自 http www iheima com article 178452 html 对于纵向轨迹的采样 我们需要考虑巡航 跟车或超车 停车这三种状态 作者 许珂诚 编辑 Natalie 大家好 我是来自百度智能驾驶事业群的许珂诚 今天很高
  • Android中使用log4j2

    Log4j2 的配置 使用 最近公司让调研log4j2在Android中的使用 在网上查了很多资料 在这做个总结 一起学习 参考了许多文章 文末都有链接 感谢大佬们的文章 Log4j2 简介 log4j2是log4j 1 x 的升级版 20
  • 多租户分库分表技术文档

    分库分表技术文档 2022 07 13 李某某 1需求 1 1多租户实现分库分表 1 2系统实现主从数据源 2系统功能 2 1多租户实现分库分表 2 1 1功能描述 根据租户的数量和租户的自定义编号实现对应的分库和分表 假设现有租户1001
  • C ++中的std :: nth_element()

    The standard library of C has a huge number of functions that are not so explored but can be very handy in case of speci