C++ STL nth_element 用法

一:功能

        将一个序列分为两组,前一组元素都小于*nth,后一组元素都大于*nth, 并且确保第 nth 个位置就是排序之后所处的位置。即该位置的元素是该序列中第nth小的数。

二:用法

#include <vector>
#include <algorithm>
#include <iostream>

int main() {
std::vector<int> data{9, 1, 8, 2, 7, 3, 6, 4, 5};
std::nth_element(data.begin(), data.begin() + 4, data.end());
for(auto e : data)
    std::cout << e << " ";
std::cout << std::endl;    

std::cout << "data[4] == " << data[4] << "\n";

std::nth_element(data.begin(), data.begin() + 7, data.end(),
    std::greater<>());

for(auto e : data)
    std::cout << e << " ";
std::cout << std::endl;   

std::cout << "data[7] == " << data[7] << "\n";
 
}

相关推荐

  1. new Promise

    2024-07-22 22:28:02       43 阅读
  2. qt 定时器

    2024-07-22 22:28:02       54 阅读
  3. fmt

    2024-07-22 22:28:02       52 阅读
  4. not exists

    2024-07-22 22:28:02       53 阅读
  5. 详解WebMvcConfigurer

    2024-07-22 22:28:02       36 阅读
  6. Tinyxml基本

    2024-07-22 22:28:02       57 阅读
  7. man

    2024-07-22 22:28:02       49 阅读
  8. mybatisPlus 常见

    2024-07-22 22:28:02       41 阅读
  9. v-show

    2024-07-22 22:28:02       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 22:28:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 22:28:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 22:28:02       45 阅读
  4. Python语言-面向对象

    2024-07-22 22:28:02       55 阅读

热门阅读

  1. 低空经济“芯”挑战

    2024-07-22 22:28:02       16 阅读
  2. Python应用—给暑假熊孩子出算术题

    2024-07-22 22:28:02       17 阅读
  3. Math Reference Notes: 数学思想和方法

    2024-07-22 22:28:02       13 阅读
  4. Flask: URL 视图函数 路由

    2024-07-22 22:28:02       16 阅读
  5. web前端 React 框架面试200题(四)

    2024-07-22 22:28:02       14 阅读
  6. Redis 持久化详解

    2024-07-22 22:28:02       15 阅读
  7. 设计模式-抽象工厂模式

    2024-07-22 22:28:02       11 阅读