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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值