Home C++ Algorithms Functions nth_element() C++ std::nth_element() Function Definition In C++, std::nth_element() rearranges a range so that the element at position nth is the one that would be there if the whole range were sorted. Everything before it is not greater, and everything after it is not smaller.
It is faster than a full sort and is perfect for finding a median or the k-th smallest value.
Syntax Add #include <algorithm>, then call std::nth_element(). Start with these two lines.
std::nth_element(v.begin(), v.begin() + 2, v.end());
std::nth_element(v.begin(), v.begin() + 2, v.end(), std::greater<int>()); Call What it does std::nth_element(v.begin(), v.begin() + 2, v.end());Find the third smallest element. std::nth_element(v.begin(), v.begin() + 2, v.end(), std::greater<int>());Split the top three from the rest.
Library declarations template<class RandomIt>
void nth_element(RandomIt first, RandomIt nth, RandomIt last);
template<class RandomIt, class Compare>
void nth_element(RandomIt first, RandomIt nth, RandomIt last, Compare comp); Parameters Parameter Description first, last Required. The range to rearrange. nth Required. The position that must end up holding the right element. comp Optional. A comparison function.
Return Value Return Value Description voidNothing is returned. Read the result at *nth.
C++ Examples 1. Find the median 2. Third smallest 3. Top three Example 1 — Find the median Get the middle value of five numbers.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {7, 2, 9, 4, 5};
auto mid = v.begin() + v.size() / 2;
std::nth_element(v.begin(), mid, v.end());
std::cout << "Median: " << *mid << '\n';
return 0;
} How It Works The vector has five elements, so the middle index is 2. After nth_element(), the element at index 2 is what a full sort would put there. A sorted copy would be 2 4 5 7 9, and the middle is 5. Example 2 — Third smallest Find the k-th smallest number.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {10, 3, 8, 1, 6, 4};
std::nth_element(v.begin(), v.begin() + 2, v.end());
std::cout << "Third smallest: " << v[2] << '\n';
return 0;
} How It Works Index 2 is the third position, because counting starts at 0. Sorted, the numbers would be 1 3 4 6 8 10. So index 2 is 4. The function did not have to sort everything. Example 3 — Top three Split the largest three from the rest, then sort them.
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 9, 3, 7};
std::nth_element(v.begin(), v.begin() + 2, v.end(), std::greater<int>());
std::sort(v.begin(), v.begin() + 3, std::greater<int>());
std::cout << "Top three: ";
for (auto it = v.begin(); it != v.begin() + 3; ++it) std::cout << *it << ' ';
std::cout << '\n';
return 0;
} How It Works With std::greater, the "first" positions hold the biggest values. nth_element() makes sure 9, 7 and 5 sit in the first three slots, in any order.A small sort() on just those three puts them in order. Frequently Asked Questions What does std::nth_element() do? In C++, std::nth_element() puts the element that would be at position n in a sorted range into place. Include <algorithm> to use it.
Which header and C++ version do I need for std::nth_element()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::nth_element() return? It returns nothing (void). The element at the nth position is the one that would be there in a sorted range.
How do I find the median with nth_element()? Choose the middle position, call nth_element(), then read the element at that position. For an even count, take the two middle values.
Is the rest of the vector sorted? No. Elements before nth are not greater, and elements after are not smaller, but neither side is sorted.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
24 people found this page helpful
Helpful Share Copy link Suggestion