C++
'nth_element' Is Not a Member of 'std' Fix
std::nth_element lives in one header, <algorithm> - and on this library version almost no ordinary file rescues it by accident: in the sweep probed here, covering all 106 standard headers in the library's header set, only <functional>, <chrono>, <execution>, <regex>, and <valarray> pull the name in on the side, and the textbook combination of <iostream> and <vector> does not. The error therefore surfaces in textbook files as well as lean ones, and in the sweep probed here it splits three ways - the plain error, the same error with a misleading 'did you mean 'tuple_element'?' tail, or one of those five transitive rescues - with no note lines, no help lines, and no link-failure shape behind any probed header. This guide pins down all three outcomes, which probed headers cause each one, and the one include that fixes the error by specification.
Written December 1, 2026 · Tested with GCC 13.3.0 · 23 min read
- The error: the include is missing, in a lean file
- A neighboring header does not rescue it: only <utility>
- The textbook version fails too - with a misleading suggestion
- Neighboring headers: which ones fail plainly, which ones add the suggestion, which ones rescue it
Written December 1, 2026
Tested with GCC 13.3.0
What this guide is
std::nth_element is a function template defined in the header <algorithm>. It rearranges the range [first, last) so that the element pointed to by nth is the element that would be there if the whole range were sorted, every element before nth compares less than or equal to it, and every element after it compares greater than or equal to it - without fully sorting either side. It returns nothing. The plain overload is constexpr since C++20; a second family of overloads, since C++17, takes an execution policy as its first argument, and a comparator overload orders by a caller-supplied comparison instead of operator<. Its iterators must be random-access iterators, and it makes O(N) comparisons on average. To fully sort only the first part of a range instead, std::partial_sort is the tool.
Because the template lives in one specific header, 'nth_element' is not a member of 'std' has one common cause:
- The
#include <algorithm>line is missing, and no other header in the file pulls the name in transitively. For this error, in the contexts probed here, GCC never prints a note naming<algorithm>, and it never prints note lines or help lines under the error - but in some contexts it does print a suggestion tail on the error line, and the suggestion is a trap:did you mean 'tuple_element'?points atstd::tuple_element, a class template in<tuple>for naming the type of a tuple's element, not at the algorithm the program means. The neighboring names probed here -std::reverse,std::partition, andstd::sort, all in the same header - get no suggestion at all in the same lean context, as the neighboring-names section shows.
What makes this error worth separating from its relatives is the shape of the sweep behind it. The sweep probed here covers every one of the 106 standard headers in this library's header set, compiled as the only header in the program, and the outcome splits three ways - the plain compile error, the same error with the tuple_element suggestion tail, or a transitive rescue - where its closest siblings in this family, such as std::rotate, split only two ways, with no suggestion class at all. The suggestion class here is large - 51 of the 100 failing sole headers - and it is exactly the headers that pull std::tuple_element into scope on the side, <tuple> itself among them. The rescue side, by contrast, matches the family pattern exactly: std::rotate and std::partition, probed in their own guides, are rescued by <functional>, <chrono>, <execution>, <regex>, and <valarray>, and std::nth_element is rescued by the same five, no more, across the full header set. The older siblings in this family - std::rotate, std::reverse, std::partition, std::sort, and std::find - have their own guides and are linked at the end, not re-taught here.
The error: the include is missing, in a lean file
This program partitions an array around its third element with std::nth_element and returns success if that element lands at its sorted value, 3, but includes only <limits>:
#include <limits>
int main() {
int a[] = {5, 1, 4, 2, 3, 6};
std::nth_element(a, a + 2, a + 6);
return a[2] == 3 ? 0 : 1;
}
$ g++ -std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:10: error: 'nth_element' is not a member of 'std'
4 | std::nth_element(a, a + 2, a + 6);
| ^~~~~~~~~~~
That is the whole output: one error, no suggestion on the error line, no note lines, no help lines. The same program with no include at all fails the same way:
int main() {
int a[] = {5, 1, 4, 2, 3, 6};
std::nth_element(a, a + 2, a + 6);
return a[2] == 3 ? 0 : 1;
}
$ g++ -std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:10: error: 'nth_element' is not a member of 'std'
3 | std::nth_element(a, a + 2, a + 6);
| ^~~~~~~~~~~
A neighboring header does not rescue it: only <utility>
A reader coming from the std::swap error might reach for <utility> first. In the program probed here it does not rescue the call - and it changes the error's shape, adding the tuple_element suggestion tail:
#include <utility>
int main() {
int a[] = {5, 1, 4, 2, 3, 6};
std::nth_element(a, a + 2, a + 6);
return a[2] == 3 ? 0 : 1;
}
$ g++ -std=c++20 u_utility.cpp -o u_utility_prog
u_utility.cpp: In function 'int main()':
u_utility.cpp:4:10: error: 'nth_element' is not a member of 'std'; did you mean 'tuple_element'?
4 | std::nth_element(a, a + 2, a + 6);
| ^~~~~~~~~~~
| tuple_element
That is expected once the homes are separated: <utility> declares std::swap, and std::nth_element is not one of its names. The suggestion appears here because <utility> pulls std::tuple_element into scope on the side, and tuple_element is close enough to nth_element in spelling for GCC to offer it - it is still the wrong name, a type trait, not a function. The two errors share a shape and a sentence pattern, but each name takes its own header, and the fix below is <algorithm>, not the swap fix.
The textbook version fails too - with a misleading suggestion
The version most often written first - partition a vector around its third element and print that element, with std::cout - gets the same error, without <algorithm>:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::nth_element(v.begin(), v.begin() + 2, v.end());
std::cout << v[2] << "\n";
return 0;
}
$ g++ -std=c++20 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:10: error: 'nth_element' is not a member of 'std'; did you mean 'tuple_element'?
5 | std::nth_element(v.begin(), v.begin() + 2, v.end());
| ^~~~~~~~~~~
| tuple_element
What happened: on this library version, neither <vector> nor <iostream> pulls this name in transitively, so the call fails in a textbook file just as it does in a lean one. The error here is not plain, though: it carries the did you mean 'tuple_element'? tail, because the textbook headers pull std::tuple_element into scope. Do not follow that suggestion - std::tuple_element names a type, takes no iterators, and calling it fixes nothing. The fix for the name the program means is the include below. In the sweep probed here, the only headers that rescue std::nth_element are <functional>, <chrono>, <execution>, <regex>, and <valarray>, besides <algorithm> itself, and that rescue is a property of this library version, not a rule of the language. A program that uses std::nth_element includes <algorithm>, whether or not it happens to compile without it.
Neighboring headers: which ones fail plainly, which ones add the suggestion, which ones rescue it
In a sweep where each of the 106 standard headers in this library's header set was compiled as the only header in a program that partitions an array around its third element and returns whether that element holds its sorted value, 3, the outcome splits by compile result and diagnostic shape three ways. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 49 headers fail with the plain is not a member of 'std' error, with no suggestion, no note, and no help:
<atomic><bit><cassert><cctype><cerrno><cfenv><cfloat><charconv><cinttypes><ciso646><climits><clocale><compare><concepts><coroutine><csetjmp><csignal><cstdalign><cstdarg><cstdbool><cstddef><cstdint><cstdio><cstdlib><cstring><ctime><cuchar><cwchar><cwctype><exception><expected><initializer_list><iosfwd><latch><limits><new><numbers><numeric><ratio><semaphore><shared_mutex><source_location><spanstream><stacktrace><stdfloat><type_traits><typeindex><typeinfo><version>
A file with no include at all fails the same plain way. And these 51 headers fail with the same error plus the suggestion tail ; did you mean 'tuple_element'? on the error line - still with no note lines and no help lines:
<any><array><barrier><bitset><ccomplex><cmath><codecvt><complex><condition_variable><ctgmath><deque><filesystem><format><forward_list><fstream><future><iomanip><ios><iostream><istream><iterator><list><locale><map><memory><memory_resource><mutex><optional><ostream><queue><random><ranges><scoped_allocator><set><span><sstream><stack><stdexcept><stop_token><streambuf><string><string_view><syncstream><system_error><thread><tuple><unordered_map><unordered_set><utility><variant><vector>
A sole <vector> file shows that second shape in full:
#include <vector>
int main() {
int a[] = {5, 1, 4, 2, 3, 6};
std::nth_element(a, a + 2, a + 6);
return a[2] == 3 ? 0 : 1;
}
$ g++ -std=c++20 v_vector.cpp -o v_vector_prog
v_vector.cpp: In function 'int main()':
v_vector.cpp:4:10: error: 'nth_element' is not a member of 'std'; did you mean 'tuple_element'?
4 | std::nth_element(a, a + 2, a + 6);
| ^~~~~~~~~~~
| tuple_element
And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><chrono><execution><functional><regex><valarray>
Two contrasts with the neighboring sweeps are worth holding on to. First, there is no link-failure shape in this sweep: every failing sole header fails at compile time with the error above - each one was also compiled to an object file on its own, and that step failed the same way - so the declared-but-not-defined link failure that std::swap produces behind five headers has no counterpart for this name in the probed set. Second, the suggestion class is this name's own trap, and it has a precise cause: the 51 headers in that class are the ones that make std::tuple_element visible on the side, and GCC offers the closest visible spelling. <tuple> itself is in the suggestion class, not the rescue class - including the header the suggestion's name lives in still does not declare std::nth_element. <semaphore>, which adds a did you mean 'binary_semaphore'? tail to the std::binary_search error, fails plainly here, and <ranges> - the header a reader might expect to carry the ranges form - is in the suggestion class for the plain name. The closest relative, std::partial_sort, was swept on its own over ten sole headers: it fails plainly with <limits>, <vector>, <iostream>, and <ranges>, and is rescued by <algorithm>, <functional>, <chrono>, <execution>, <regex>, and <valarray> - so no probed header in that set makes partial_sort visible while nth_element stays missing. The rescue list, though, matches the short lists in this family: <algorithm> - the home header - is on this list by definition, not by transitivity, and every other rescue on that list is this library version's behavior, not a guarantee.
The fix
Add the header:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::nth_element(v.begin(), v.begin() + 2, v.end());
std::cout << v[2];
for (int x : v) std::cout << " " << x;
std::cout << "\n";
return 0;
}
$ g++ -std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
3 2 1 3 4 5 6
One include, <algorithm>, declares the template. Partitioning 5, 1, 4, 2, 3, 6 around its third element puts 3 - the value a full sort would place there - at index 2, with the smaller elements 2, 1 before it and the larger elements 4, 5, 6 after it, in no particular order on either side, so the program prints 3 followed by the partitioned contents.
The fixed programs: the element follows the position
With <algorithm> added, std::nth_element answers to whatever position the program passes. Partitioning three copies of the same vector at positions 0, 2, and 5 puts the sorted values for those positions - 1, 3, and 6 - at those indexes:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> a = {5, 1, 4, 2, 3, 6};
std::vector<int> b = {5, 1, 4, 2, 3, 6};
std::vector<int> c = {5, 1, 4, 2, 3, 6};
std::nth_element(a.begin(), a.begin(), a.end());
std::nth_element(b.begin(), b.begin() + 2, b.end());
std::nth_element(c.begin(), c.begin() + 5, c.end());
std::cout << a[0] << " " << b[2] << " " << c[5] << "\n";
return 0;
}
$ g++ -std=c++20 i_cases.cpp -o i_cases_prog
$ ./i_cases_prog
1 3 6
The two boundary positions behave as the rule says. When nth equals last, the call is allowed and rearranges nothing the program can observe - the vector still holds a permutation of its elements; when nth equals first, the smallest element lands at the front - 1 here:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> a = {5, 1, 4, 2, 3, 6};
std::vector<int> b = {5, 1, 4, 2, 3, 6};
std::nth_element(a.begin(), a.end(), a.end());
std::nth_element(b.begin(), b.begin(), b.end());
bool ok = true;
for (int x : a) ok = ok && (x >= 1 && x <= 6);
std::cout << b[0] << " " << ok << "\n";
return 0;
}
$ g++ -std=c++20 k_edges.cpp -o k_edges_prog
$ ./k_edges_prog
1 1
The partition property is exact, not just a correct element at the position: after partitioning by position 2, the element there is 3, every element before it is at most 3, and every element after it is at least 3, which this program checks element by element and reports as 1, alongside the element itself:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::nth_element(v.begin(), v.begin() + 2, v.end());
bool ok = (v[2] == 3);
for (int i = 0; i < 2; ++i) ok = ok && (v[i] <= v[2]);
for (int i = 3; i < 6; ++i) ok = ok && (v[i] >= v[2]);
std::cout << ok << " " << v[2] << "\n";
return 0;
}
$ g++ -std=c++20 k_values.cpp -o k_values_prog
$ ./k_values_prog
1 3
The execution-policy overload - the second family of overloads, since C++17 - takes the policy as its first argument and needs <execution> for the policy itself. With std::execution::par the same partitioning puts the same value, 3, at index 2:
#include <algorithm>
#include <execution>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::nth_element(std::execution::par, v.begin(), v.begin() + 2, v.end());
std::cout << v[2] << "\n";
return 0;
}
$ g++ -std=c++20 j_policy.cpp -o j_policy_prog
$ ./j_policy_prog
3
The comparator overload orders by the comparison the program passes instead: with std::greater<int>() from <functional>, partitioning around position 1 puts the second-largest value, 5, there, with the largest, 6, before it:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::nth_element(v.begin(), v.begin() + 1, v.end(), std::greater<int>());
std::cout << v[0] << " " << v[1] << "\n";
return 0;
}
$ g++ -std=c++20 j_comp.cpp -o j_comp_prog
$ ./j_comp_prog
6 5
The neighboring names: same header, plain in the lean context
The names that std::nth_element is built alongside share its home header. In the lean context here - only <limits> included - they get no suggestion at all, unlike nth_element in its suggestion-class contexts. std::reverse - the function template defined in <algorithm> that reverses the order of the elements in a range - gets its own error, again with no header note:
#include <limits>
int main() {
int a[] = {1, 2, 3, 4, 5, 6};
std::reverse(a, a + 6);
return a[0] == 6 ? 0 : 1;
}
$ g++ -std=c++20 b_reverse.cpp -o b_reverse_prog
b_reverse.cpp: In function 'int main()':
b_reverse.cpp:4:10: error: 'reverse' is not a member of 'std'
4 | std::reverse(a, a + 6);
| ^~~~~~~
std::partition - the function template defined in <algorithm> that reorders a range so the elements satisfying a predicate come first - fails the same way:
#include <limits>
int main() {
int a[] = {5, 1, 4, 2, 3, 6};
return std::partition(a, a + 6, [](int x) { return x % 2 == 0; }) - a == 3 ? 0 : 1;
}
$ g++ -std=c++20 c_partition.cpp -o c_partition_prog
c_partition.cpp: In function 'int main()':
c_partition.cpp:4:17: error: 'partition' is not a member of 'std'
4 | return std::partition(a, a + 6, [](int x) { return x % 2 == 0; }) - a == 3 ? 0 : 1;
| ^~~~~~~~~
std::sort - the function template defined in <algorithm> that sorts a range into ascending order - fails the same way:
#include <limits>
int main() {
int a[] = {5, 1, 4, 2, 3, 6};
std::sort(a, a + 6);
return a[0] == 1 ? 0 : 1;
}
$ g++ -std=c++20 d_sort.cpp -o d_sort_prog
d_sort.cpp: In function 'int main()':
d_sort.cpp:4:10: error: 'sort' is not a member of 'std'
4 | std::sort(a, a + 6);
| ^~~~
And the closest relative of all, std::partial_sort - the function template defined in <algorithm> that sorts the first part of a range in place, leaving the rest unordered - fails the same way, again with no header note:
#include <limits>
int main() {
int a[] = {5, 1, 4, 2, 3, 6};
std::partial_sort(a, a + 3, a + 6);
return a[0] == 1 && a[2] == 3 ? 0 : 1;
}
$ g++ -std=c++20 p_partial_lean.cpp -o p_partial_lean_prog
p_partial_lean.cpp: In function 'int main()':
p_partial_lean.cpp:4:10: error: 'partial_sort' is not a member of 'std'
4 | std::partial_sort(a, a + 3, a + 6);
| ^~~~~~~~~~~~
One contrast is worth holding on to: in the lean programs here, nth_element, reverse, partition, sort, and partial_sort all get no note and - in this <limits> context - no suggestion. The tuple_element suggestion appears only in contexts where a header in the file makes that name visible, as the sweep section shows; do not read its presence as a fix, or its absence as a puzzle - in this probed set the fix comes from the names' home, <algorithm>, either way.
The neighboring names, fixed
And the neighboring names work under the same one added header: sorting 5, 1, 4, 2, 3, 6 and then reversing it puts 6 first and 1 last, and partitioning that descending range by even puts the partition point at index 3:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::sort(v.begin(), v.end());
std::reverse(v.begin(), v.end());
std::cout << v[0] << " " << v[5] << " " << (std::partition(v.begin(), v.end(), [](int x) { return x % 2 == 0; }) - v.begin()) << "\n";
return 0;
}
$ g++ -std=c++20 k_neighbors_fixed.cpp -o k_neighbors_fixed_prog
$ ./k_neighbors_fixed_prog
6 1 3
std::partial_sort sorts the first three elements and leaves the rest unordered: the first three positions hold 1, 2, 3 in order:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::partial_sort(v.begin(), v.begin() + 3, v.end());
std::cout << v[0] << " " << v[1] << " " << v[2] << "\n";
return 0;
}
$ g++ -std=c++20 k_partial_fixed.cpp -o k_partial_fixed_prog
$ ./k_partial_fixed_prog
1 2 3
Since C++20 the same <algorithm> header also declares std::ranges::nth_element, the ranges form of the same partitioning. It comes in two call shapes - an iterator triple, and a range plus the middle iterator - and returns the past-the-end iterator of the range: here both shapes put 3 at index 2. In the lean context - only <limits> included - it fails with its own wording, 'std::ranges' has not been declared, because the namespace itself is not declared there:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::vector<int> w = {5, 1, 4, 2, 3, 6};
std::ranges::nth_element(v.begin(), v.begin() + 2, v.end());
std::ranges::nth_element(w, w.begin() + 2);
std::cout << v[2] << " " << w[2] << "\n";
return 0;
}
$ g++ -std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
3 3
#include <limits>
int main() {
int a[] = {5, 1, 4, 2, 3, 6};
std::ranges::nth_element(a, a + 2, a + 6);
return a[2] == 3 ? 0 : 1;
}
$ g++ -std=c++20 r2_ranges_lean.cpp -o r2_ranges_lean_prog
r2_ranges_lean.cpp: In function 'int main()':
r2_ranges_lean.cpp:4:10: error: 'std::ranges' has not been declared
4 | std::ranges::nth_element(a, a + 2, a + 6);
| ^~~~~~
A different wording - and a case where the qualifier is not needed
Including <algorithm> is not the only half of the name - but for this function the unqualified form behaves two ways, and which one a program gets depends on its arguments. This program includes the header, drops the std:: qualifier, and passes raw pointers into a C array:
#include <algorithm>
int main() {
int a[] = {5, 1, 4, 2, 3, 6};
nth_element(a, a + 2, a + 6);
return a[2] == 3 ? 0 : 1;
}
$ g++ -std=c++20 g2_unqualified_ptr.cpp -o g2_unqualified_ptr_prog
g2_unqualified_ptr.cpp: In function 'int main()':
g2_unqualified_ptr.cpp:4:5: error: 'nth_element' was not declared in this scope
4 | nth_element(a, a + 2, a + 6);
| ^~~~~~~~~~~
g2_unqualified_ptr.cpp:4:5: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:73,
from g2_unqualified_ptr.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:405:1: note: 'std::nth_element'
405 | nth_element(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __nth,
| ^~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:2047:37: note: 'std::ranges::nth_element'
2047 | inline constexpr __nth_element_fn nth_element{};
| ^~~~~~~~~~~
That wording - was not declared in this scope - is a different defect with a different fix: the header is present, the qualifier is not, and raw pointers belong to no namespace, so no lookup finds the name. Here the notes do name the right names - the first points at std::nth_element itself, shown at its execution-policy declaration in the internal header the transcript names, and the second at std::ranges::nth_element. Qualify the name as std::nth_element; adding includes does not bring the name out of namespace std.
The same unqualified call with std::vector iterators as its arguments behaves differently - it compiles and runs, printing 3:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
nth_element(v.begin(), v.begin() + 2, v.end());
std::cout << v[2] << "\n";
return 0;
}
$ g++ -std=c++20 g_unqualified.cpp -o g_unqualified_prog
$ ./g_unqualified_prog
3
What happened: the iterators belong to namespace std, so argument-dependent lookup finds std::nth_element from the arguments themselves, even without the qualifier. That is a property of the argument types, not a style to rely on: the same call over raw pointers fails, as the transcript above shows. Write the qualifier either way.
The exact rule
std::nth_elementis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , - ,
, 'nth_element' is not a member of 'std'means the<algorithm>include is missing and nothing else in the file pulled the name in. In the contexts probed here, GCC prints no note naming<algorithm>, no note lines, and no help lines under it; the only diagnostic tail in the probed set is the suggestiondid you mean 'tuple_element'?, which appears in the 51 sole-header contexts listed in the sweep section and in the textbook context, and names a class template in<tuple>, not the algorithm.- In the sweep probed here, the split is three-way: a sole header either fails plainly, fails with the
tuple_elementsuggestion tail, or rescues the program. Each failing sole header also failed when compiled to an object file on its own, so the probed set contains no declared-but-not-defined link-failure shape for this name. - In the sweep probed here, only five headers besides
<algorithm>rescuestd::nth_elementtransitively -<functional>,<chrono>,<execution>,<regex>, and<valarray>- so a file whose only include is one of those compiles and runs the call without<algorithm>. That is this library version's behavior, not a guarantee - in the probed set, those headers are not required to pullstd::nth_elementin. std::reverse,std::partition,std::sort, andstd::partial_sortare declared by the same#include <algorithm>: in the programs shown here, which include only<limits>beside the use, each gets its ownis not a member of 'std'error when that include is missing, with no header note and no suggestion.std::ranges::nth_elementis declared by the same#include <algorithm>since C++20: with the include it partitions the range and returns the past-the-end iterator, in both its iterator-triple and its range-plus-middle call shapes, and in the lean context probed here it gets the error'std::ranges' has not been declaredwhen that include is missing.'nth_element' was not declared in this scopefor an unqualifiednth_elementuse over raw pointers is a different defect: qualify the name asstd::nth_element- the include alone does not bring the name out of namespacestd. The same unqualified use overstd::vectoriterators compiles in the program probed here through argument-dependent lookup; qualify the name there too, so the call does not depend on its argument types.
The fixes, ranked
- First, check the include list of the file that names
std::nth_element. If<algorithm>is not there, add it - that is the fix in the common case, and it holds whether the file failed plainly, failed with the suggestion tail, or happened to compile before through a transitive rescue. - Second, do not follow the
tuple_elementsuggestion. In the contexts probed here where it appears, it names a type trait in<tuple>- it cannot be called with iterators, and adding<tuple>does not declarestd::nth_elementeither: in the sweep here<tuple>itself is in the suggestion class, not the rescue class. - Third, do not reach for
<utility>because the error sentence looks like thestd::swaperror. In the program probed here,<utility>as the only include fails with the same error, plus the suggestion tail - the two names live in different headers, and each takes its own. - Fourth, do not be reassured by
<iostream>,<vector>, or<string>being present - in the sweep here none of them rescues this name at all - and do not remove<algorithm>because a file compiles without it: in the probed set only<functional>,<chrono>,<execution>,<regex>, and<valarray>rescue it, and that rescue is not guaranteed. - Fifth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::nth_elementas well - and do not rely on an unqualified call that happens to compile overstditerators, because the same call over raw pointers fails. - Sixth, if the failing name is a neighboring one, check its header before fixing:
reverse,partition,sort, andpartial_sorttake the same added<algorithm>, whileswaptakes<utility>.
Which fix for which cause
#include <algorithm>missing,std::nth_elementused in a file whose only include is one of the 49 headers listed as plain in the sweep section above, or that includes nothing: the message is'nth_element' is not a member of 'std', with no suggestion, no header note, and no help - add#include <algorithm>.#include <algorithm>missing,std::nth_elementused in a file whose only include is one of the 51 headers listed as suggestion-class in the sweep section above: the message is the same error with the tail; did you mean 'tuple_element'?- ignore the suggestion and add#include <algorithm>.std::nth_elementused in a textbook file including<iostream>and<vector>: the message is the same error, with thetuple_elementsuggestion tail - add#include <algorithm>.std::nth_elementused with only<algorithm>,<functional>,<chrono>,<execution>,<regex>, or<valarray>included: no error on this library version - add#include <algorithm>anyway when it is not the include already present, because the rescue is not guaranteed.std::reverse,std::partition,std::sort, orstd::partial_sortused without<algorithm>in the lean context: the message names that name, plainly, with no header note - add#include <algorithm>.std::ranges::nth_elementused without<algorithm>in the lean context: the message is'std::ranges' has not been declared- add#include <algorithm>.nth_elementused without thestd::qualifier,<algorithm>included, over raw pointers: the message is'nth_element' was not declared in this scope- qualify the name asstd::nth_element. Overstd::vectoriterators the same unqualified call compiles through argument-dependent lookup in the program probed here - qualify it anyway.
Checklist
- Check the include list of the file that names the partitioning, not just the project. The include must be in that file, or in a header that file includes directly by design.
- Do not follow the
tuple_elementsuggestion, and do not wait for a note naming the header. For this error, in the contexts probed here, GCC prints no note naming<algorithm>- take the fix from the name's home header,<algorithm>, instead. - Do not import the
std::swapfix into this error: in the probed set,<utility>fails here too, with the suggestion tail added. Split neighboring names by home header first. - Do not assume the textbook includes rescue this name, and do not delete a working
<algorithm>line because a file compiles without it. In the sweep here<iostream>and<vector>do not rescuestd::nth_elementat all, and the five headers that do rescue it do so only transitively. - One header per name the file uses:
std::vectorcomes from<vector>andstd::stringcomes from<string>, even in a program whose subject is the partitioning - andstd::nth_elementcomes from<algorithm>in return. The execution-policy overload additionally takes<execution>for the policy object itself, and the comparator overload'sstd::greatercomes from<functional>. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
3 2 1 3 4 5 6for the element at the position and the partitioned contents, and1 3for the partition-property check and the element it found.
Related guides
- 'std::rotate' Is Not a Member of 'std' Fix - the same missing-include shape for the left-rotation function template in
<algorithm>: its full header sweep splits only two ways, with no suggestion class, where this guide's sweep splits three ways. - 'std::reverse' Is Not a Member of 'std' Fix - the same missing-include shape for the order-reversing function template in
<algorithm>, an older sibling in this family whose own guide covers its error and its fix. - 'std::partition' Is Not a Member of 'std' Fix - the same missing-include shape for the predicate reordering in
<algorithm>: its sweep produced exactly the same five transitive rescues as this guide's full header sweep, and its textbook file fails plainly where this guide's fails with the suggestion tail. - 'std::sort' Is Not a Member of 'std' Fix - the same missing-include shape for the sorting function template in
<algorithm>, an older sibling in this family whose own guide covers its error and its fix.
Sources
- cppreference,
std::nth_element— forstd::nth_elementbeing a function template defined in<algorithm>that rearranges[first, last)so the element atnthis the element that would be there if the range were sorted, with every element beforenthcomparing less than or equal to it and every element after it comparing greater than or equal to it, returning nothing, with the plain overloadsconstexprsince C++20, execution-policy overloads since C++17, random-access iterators required, and O(N) comparisons on average. - cppreference, Standard library header
<algorithm>— for<algorithm>being the header that declaresstd::nth_elementand the neighboring algorithm names, including thestd::ranges::nth_elementalgorithm function object since C++20. - cppreference,
std::ranges::nth_element— forstd::ranges::nth_elementbeing an algorithm function object defined in<algorithm>since C++20, in an iterator-triple form and a range-plus-middle form, that performs the same rearrangement and returns the past-the-end iterator of the range. - cppreference,
std::partial_sort— forstd::partial_sortbeing a function template defined in<algorithm>that sorts the first part of a range in place, leaving the rest of the range unordered. - cppreference,
std::reverse— forstd::reversebeing a function template defined in<algorithm>that reverses the order of the elements in a range. - cppreference,
std::partition— forstd::partitionbeing a function template defined in<algorithm>that reorders the elements in[first, last)so all elements for which the unary predicate returnstruecome before all elements for which it returnsfalse, returning the iterator to the partition point. - cppreference,
std::sort— forstd::sortbeing a function template defined in<algorithm>that sorts the elements in a range into ascending order.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.