#include <iostream>
#include <algorithm>
int main() {
int arr[] = {9, 4, 7, 2, 5, 1, 6, 3, 8};
int k = 3; // 找第3小的元素
std::nth_element(arr, arr + k - 1, arr + 9);
std::cout << "第" << k << "小的元素是:" << arr[k - 1] << std::endl;
return 0;
}
#include <iostream>
#include <algorithm>
int main() {
int arr[] = {9, 4, 7, 2, 5, 1, 6, 3, 8};
int k = 3; // 找第3大的元素
std::nth_element(arr, arr + 9 - k, arr + 9);
std::cout << "第" << k << "大的元素是:" << arr[9 - k] << std::endl;
return 0;
}