C++
'std::max_element' Is Not a Member of 'std' Fix
std::max_element lives in one header, <algorithm> - and in the sweep probed here, covering all 106 standard headers in this library's header set, only 4 other headers pull it in on the side: <execution>, <functional>, <regex>, and <valarray>. That is why this error is an ordinary-file error: the textbook combination of <iostream> and <vector> fails, and for 45 of the failing headers the diagnostic makes it worse, suggesting 'tuple_element' - a name from a different header that has nothing to do with finding a largest element. This guide pins down the failing shapes, the 4 transitive rescues, the semantics the fixed call gives back, and the one include that fixes it by rule.
Written December 2, 2026 · Tested with GCC 13.3.0 · 26 min read
- The error: the include is missing
- The textbook version fails too - and its diagnostic points the wrong way
- Neighboring headers: 101 fail in two shapes, and 4 rescue it by accident
- The fix
Written December 2, 2026
Tested with GCC 13.3.0
What this guide is
std::max_element is a function template defined in the header <algorithm>. It returns an iterator to the greatest element in a range: if several elements are equally greatest, it returns the iterator to the first of them, and if the range is empty it returns the last iterator. The iterators need only meet the forward-iterator requirements, the classic form makes exactly max(N-1, 0) comparisons over a range of N elements, and the function predates C++11, has been usable in constant expressions since C++17, and has execution-policy overloads since C++17.
Because the function lives in <algorithm> - not in a container's header, and not in <numeric> - 'max_element' is not a member of 'std' has one cause, and naming it is the first step:
- 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, that second clause rarely saves anyone: in the sweep below, only 4 of the other 105 headers in the library's header set pull the name in on the side, so most files without<algorithm>reach this error.
There is no version form of this failure for the classic call. std::max_element is an original name, older than C++11, so a file that includes <algorithm> and is compiled as C++98 finds its largest element correctly, as the versions section below shows. The pieces layered on top do have floors - constant-expression use and the execution-policy overloads both need C++17 - but a reader who raises the standard flag to chase the plain error is fixing a flag that was not the problem.
A largest-element program's file most often already holds <vector> for the container and <iostream> for printing, and in the sweep probed here neither rescues the name - so the file a reader actually has fails, textbook includes and all. The sweep 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 two ways - the failure, in two diagnostic shapes, or a rescue - and the rescue set has 5 entries: <algorithm> itself and 4 transitive rescues.
The error: the include is missing
This program finds the largest element of a five-element array, but includes only <limits>:
#include <limits>
int main() {
int a[5] = {3, 1, 4, 1, 5};
auto it = std::max_element(a, a + 5);
return *it == 5 ? 0 : 1;
}
-std=c++17 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:20: error: 'max_element' is not a member of 'std'
4 | auto it = std::max_element(a, a + 5);
| ^~~~~~~~~~~
Read it in full: one plain error line - no suggestion tail rides on it, there are no note lines, and there are no help lines. Nothing in the diagnostic names <algorithm> or points anywhere at all. The same program with no include at all fails the same way:
-std=c++17 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:20: error: 'max_element' is not a member of 'std'
3 | auto it = std::max_element(a, a + 5);
| ^~~~~~~~~~~
The textbook version fails too - and its diagnostic points the wrong way
The version most often written first - print the largest element of a vector and where it sits - produces this error as well, without <algorithm>:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5};
auto it = std::max_element(v.begin(), v.end());
std::cout << "max = " << *it << " at index " << (it - v.begin()) << "\n";
return 0;
}
-std=c++17 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:20: error: 'max_element' is not a member of 'std'; did you mean 'tuple_element'?
5 | auto it = std::max_element(v.begin(), v.end());
| ^~~~~~~~~~~
| tuple_element
It fails, and the failure carries a suggestion: did you mean 'tuple_element'? That suggestion is a trap. std::tuple_element is a type trait from <tuple> that names the type of one element of a tuple-like object; it is not a function, it does not find anything, and following the suggestion leads away from the fix. The suggestion appears because <vector> and <iostream> between them make tuple_element visible in namespace std, and the compiler offers the nearest visible name it can find - nearest in spelling, not in meaning. A sole <vector> file fails with the same suggestion:
-std=c++17 v_vector_fail.cpp -o v_vector_fail_prog
v_vector_fail.cpp: In function 'int main()':
v_vector_fail.cpp:4:20: error: 'max_element' is not a member of 'std'; did you mean 'tuple_element'?
4 | auto it = std::max_element(v.begin(), v.end());
| ^~~~~~~~~~~
| tuple_element
A program that uses std::max_element includes <algorithm>, whether or not it happens to compile without it. A sole <algorithm> file makes the point in miniature - the call compiles and runs with nothing else in sight:
-std=c++17 i_algorithm.cpp -o i_algorithm_prog
$ ./i_algorithm_prog
Neighboring headers: 101 fail in two shapes, and 4 rescue it by accident
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 finds the largest element of a five-element array and checks the result against 5, the outcome splits by compile result and diagnostic shape two ways. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 56 headers all fail with the plain is not a member of 'std' error line - no suggestion tail on that line, no note lines, and no help lines:
<atomic><barrier><bit><cassert><cctype><cerrno><cfenv><cfloat><charconv><chrono><cinttypes><ciso646><climits><clocale><compare><concepts><coroutine><csetjmp><csignal><cstdalign><cstdarg><cstdbool><cstddef><cstdint><cstdio><cstdlib><cstring><ctime><cuchar><cwchar><cwctype><exception><expected><format><initializer_list><iosfwd><latch><limits><new><numbers><numeric><ranges><ratio><semaphore><shared_mutex><source_location><span><spanstream><stacktrace><stdfloat><stop_token><syncstream><type_traits><typeindex><typeinfo><version>
And these 45 headers fail with the same error line carrying the misleading suggestion tail quoted in the textbook section above - ; did you mean 'tuple_element'?, with the suggested name repeated under the caret - and likewise no note lines and no help lines:
<any><array><bitset><ccomplex><cmath><codecvt><complex><condition_variable><ctgmath><deque><filesystem><forward_list><fstream><future><iomanip><ios><iostream><istream><iterator><list><locale><map><memory><memory_resource><mutex><optional><ostream><queue><random><scoped_allocator><set><sstream><stack><stdexcept><streambuf><string><string_view><system_error><thread><tuple><unordered_map><unordered_set><utility><variant><vector>
A file with no include at all fails plainly, with no suggestion. Three entries in these lists deserve a pause. <iterator> fails, with the suggestion: the header of the iterator operations does not declare this algorithm, however iterator-shaped its arguments look. <numeric> fails plainly: the header of the accumulating names does not declare this one either. And <ranges> fails plainly: the C++20 ranges library has its own max_element name, and it does not bring this one in - that name gets its own section below. The split between the two failing shapes is exact: a failing header produces the suggestion precisely when the header's own contents make std::tuple_element visible, which is why the container, stream, and string headers - the ones a real file is most likely to hold - are the ones whose diagnostics point the wrong way.
And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><execution><functional><regex><valarray>
The contrast with the neighboring sweeps is the whole shape of this name. For std::distance the rescue set had 45 entries, because <iterator> sits underneath the container and stream headers; for this name it has 5, because <algorithm> sits on top of them - almost nothing pulls it in on the side, and the four headers that do are headers few largest-element programs include. That is also why the failing set covers nearly the whole library: with 101 of the other 105 headers failing, the missing include is the normal state of a file that was never given <algorithm>, not an edge case. <algorithm> - the home header - is on the rescue list by definition, not by transitivity, and the other 4 rescues are 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 = {3, 1, 4, 1, 5};
auto it = std::max_element(v.begin(), v.end());
std::cout << "max = " << *it << " at index " << (it - v.begin()) << "\n";
return 0;
}
-std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
max = 5 at index 4
One include, <algorithm>, declares the function - and the textbook program, which failed outright before, now prints max = 5 at index 4: the largest element of 3, 1, 4, 1, 5 is the 5, sitting at index 4. What the include changes is the standing of the program as well as its fate: the name is now declared by a header the file names itself, instead of by an internal include of one of the four rescuing headers that a different library version is free to rearrange.
The return value: an iterator to the first of the largest
The call returns an iterator, not a value, and which element it lands on follows the range and the comparison given to it. Over a vector it lands on the largest element; over a list it does the same by stepping; when the largest value occurs twice it lands on the first occurrence; and a comparator redefines what counts as largest:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <string>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2};
std::list<int> l = {3, 1, 4, 1, 5};
std::vector<int> ties = {2, 9, 4, 9, 1};
std::vector<int> vals = {-3, 1, -14, 1, 5};
std::vector<std::string> words = {"pear", "fig", "banana"};
std::cout << "vector " << *std::max_element(v.begin(), v.end()) << "\n";
std::cout << "list " << *std::max_element(l.begin(), l.end()) << "\n";
auto t = std::max_element(ties.begin(), ties.end());
std::cout << "first-tie " << *t << " at index " << (t - ties.begin()) << "\n";
auto a = std::max_element(vals.begin(), vals.end(),
[](int x, int y) { return std::abs(x) < std::abs(y); });
std::cout << "abs " << *a << "\n";
auto s = std::max_element(words.begin(), words.end(),
[](const std::string& x, const std::string& y) { return x.size() > y.size(); });
std::cout << "shortest " << *s << "\n";
return 0;
}
-std=c++17 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
vector 9
list 5
first-tie 9 at index 1
abs -14
shortest fig
Read in order: the vector line reports 9, the largest of its seven elements, and the list line reports 5 - the same answer over a range whose iterators can only be stepped one at a time. The first-tie line reports 9 at index 1: the value 9 occurs at indices 1 and 3, and the call returns the first of the equally greatest elements, not the last. The abs line reports -14: with a comparator that orders by absolute value, the largest element is the one with the greatest absolute value, and the iterator lands on the element itself, sign included. The shortest line reports fig: the comparator given there orders longer strings as smaller, so the element the call treats as greatest is the shortest string. In every case the call returns a position in the range passed, and dereferencing it gives the element at that position.
The edge ranges: empty and one element
No short range is an error. An empty range returns the last iterator - the past-the-end iterator itself - and a one-element range returns its only element:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> e;
std::vector<int> one = {8};
auto eit = std::max_element(e.begin(), e.end());
std::cout << "empty " << (eit == e.end() ? "end" : "value") << "\n";
std::cout << "one " << *std::max_element(one.begin(), one.end()) << "\n";
return 0;
}
-std=c++17 k_empty.cpp -o k_empty_prog
$ ./k_empty_prog
empty end
one 8
The empty call reports end because there is no largest element to point at: the documented result for an empty range is the last iterator, so a caller that dereferences the result without checking has to rule the empty case out first. The one-element call reports 8 - the single element is trivially the largest. Only the empty case needs care at the call site.
Forward iterators: the weakest iterators it accepts
The iterator requirement is the forward-iterator one: the range does not need to be indexable, and it does not need to be walkable backward. This program finds the largest element of a forward list - a container whose iterators step in one direction only:
#include <algorithm>
#include <forward_list>
#include <iostream>
int main() {
std::forward_list<int> fl = {3, 1, 4, 1, 5};
std::cout << *std::max_element(fl.begin(), fl.end()) << "\n";
return 0;
}
-std=c++17 f_forward.cpp -o f_forward_prog
$ ./f_forward_prog
5
It reports 5. The scan is a single forward pass - the classic form makes exactly one comparison per element after the first - and a forward-only range is enough for it, which is the floor the list line in the semantics section also stands on.
Versions: the classic call predates C++11 - the layered pieces start at C++17
This call does not need a modern standard. The fixed programs in this guide are compiled as C++17, but the classic call has no version floor at all: with <algorithm> included, the same call compiled as C++98 compiles and runs, and compiled as C++11 it reports the same result:
#include <algorithm>
#include <iostream>
int main() {
int a[5] = {3, 1, 4, 1, 5};
int* it = std::max_element(a, a + 5);
std::cout << *it << "\n";
return 0;
}
-std=c++98 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
5
-std=c++11 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
5
Both standards print 5. So this name's error has exactly one cause with exactly one fix for the classic call, and the diagnostic cannot separate two causes because there is only one: a missing include fails under any standard - plainly or with the tuple_element suggestion, depending on the file's other includes, in every failing context probed here - and no standard flag rescues the call when the include is missing, while no file with the include present fails for the standard's reason. Unlike the iterator operations, this name does have an execution-policy overload layered on top of the classic call, since C++17 - and naming one splits in two when a reader tries it. Naming the policy with only <algorithm> included fails on the policy's name, not the algorithm's:
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5};
auto it = std::max_element(std::execution::seq, v.begin(), v.end());
return *it == 5 ? 0 : 1;
}
-std=c++17 p_policy_algonly.cpp -o p_policy_algonly_prog
p_policy_algonly.cpp: In function 'int main()':
p_policy_algonly.cpp:5:37: error: 'std::execution' has not been declared
5 | auto it = std::max_element(std::execution::seq, v.begin(), v.end());
| ^~~~~~~~~
That wording - has not been declared, naming std::execution - is the missing-header shape, and here adding the header is the whole fix: add <execution> alongside <algorithm>, and the same call compiles and runs, because the policy overloads of std::max_element exist:
#include <algorithm>
#include <execution>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5};
auto it = std::max_element(std::execution::seq, v.begin(), v.end());
return *it == 5 ? 0 : 1;
}
-std=c++17 p_policy_both.cpp -o p_policy_both_prog
$ ./p_policy_both_prog
The policy forms belong to the algorithm names, and this is one: the overloads are part of this name since C++17, so they carry that floor. Compiled as C++14, the same two-header program fails on the policy's name again - at that standard the policy names are not there to be declared:
-std=c++14 p_policy_cpp14.cpp -o p_policy_cpp14_prog
p_policy_cpp14.cpp: In function 'int main()':
p_policy_cpp14.cpp:6:37: error: 'std::execution' has not been declared
6 | auto it = std::max_element(std::execution::seq, v.begin(), v.end());
| ^~~~~~~~~
And since C++17 the classic call is also usable in constant expressions - this program's largest element is found at compile time, and the assertion on it is part of the build:
#include <algorithm>
#include <array>
constexpr bool check() {
std::array<int, 5> a = {3, 1, 4, 1, 5};
return *std::max_element(a.begin(), a.end()) == 5;
}
static_assert(check());
int main() { return 0; }
-std=c++17 c_constexpr17.cpp -o c_constexpr17_prog
$ ./c_constexpr17_prog
Compiled as C++14, the same program fails - the call is not a constant expression there, and the static assertion cannot hold - so C++17 is the floor for that layered piece and for the policy forms, and for nothing else in this guide.
The neighboring names: min_element and minmax_element live in the same header
The two names this function pairs with do not live in a container's header either. std::min_element returns an iterator to the smallest element of a range, and std::minmax_element - since C++11 - returns a pair of iterators to the smallest and the largest in one pass - and both are declared by <algorithm>, alongside std::max_element. They share this name's failure as well, in both of its shapes: in a file whose only include is <vector>, a std::min_element call fails with the same misleading suggestion as this guide's name:
-std=c++17 b_min_vector.cpp -o b_min_vector_prog
b_min_vector.cpp: In function 'int main()':
b_min_vector.cpp:4:20: error: 'min_element' is not a member of 'std'; did you mean 'tuple_element'?
4 | auto it = std::min_element(v.begin(), v.end());
| ^~~~~~~~~~~
| tuple_element
- and a
std::minmax_elementcall in the same file fails with the plain error, no suggestion at all:
-std=c++17 b_minmax_vector.cpp -o b_minmax_vector_prog
b_minmax_vector.cpp: In function 'int main()':
b_minmax_vector.cpp:4:19: error: 'minmax_element' is not a member of 'std'
4 | auto p = std::minmax_element(v.begin(), v.end());
| ^~~~~~~~~~~~~~
They are shown here only as neighbors, not re-taught.
The neighboring names, fixed
The three names together take the same one added header - <algorithm> covers all of them, and <vector> covers only the container they walk. The fixed neighbor program reports min 1, max 9, and minmax 1 9 over 3, 1, 4, 1, 5, 9, 2:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2};
auto mm = std::minmax_element(v.begin(), v.end());
std::cout << "min " << *std::min_element(v.begin(), v.end()) << "\n";
std::cout << "max " << *std::max_element(v.begin(), v.end()) << "\n";
std::cout << "minmax " << *mm.first << " " << *mm.second << "\n";
return 0;
}
-std=c++17 b_neighbours_fixed.cpp -o b_neighbours_fixed_prog
$ ./b_neighbours_fixed_prog
min 1
max 9
minmax 1 9
Read in order: the smallest element is 1 and the largest is 9, and the paired call returns both positions in one pass - minmax 1 9 dereferences to the same two values. One header declares all three extrema operations, and a complete extrema program needs it alongside the container's own header.
A ranges form since C++20, in the same header
C++20 added a ranges form of this name, and it lives in the same header as the classic name - not in <ranges>. std::ranges::max_element is a function object in the ranges namespace, declared in <algorithm>, that also accepts a whole range. Compiled as C++20 it reports 9 for the same seven-element vector the semantics program scans:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2};
auto it = std::ranges::max_element(v);
std::cout << *it << "\n";
return 0;
}
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
9
Two boundaries keep the forms separate. The ranges form needs C++20: compiled as C++17, that same program fails on the namespace itself, with 'std::ranges' has not been declared. And the ranges header is not where the ranges form lives: a file that includes <ranges> and <array> but not <algorithm> fails for it, with an error that names the namespace the reader used:
-std=c++20 r_ranges_rangesheader.cpp -o r_ranges_rangesheader_prog
r_ranges_rangesheader.cpp: In function 'int main()':
r_ranges_rangesheader.cpp:5:28: error: 'max_element' is not a member of 'std::ranges'
5 | auto it = std::ranges::max_element(a);
| ^~~~~~~~~~~
Both forms take <algorithm>; only the classic form is available before C++20. And the classic name is not pulled in by the ranges header either: in the sweep probed here, a file whose only include is <ranges> fails for std::max_element with the plain error, as the sweep section above lists.
A different wording - and an argument-dependent rescue that depends on the iterators' types
Including <algorithm> is not the only half of the name. Over plain arrays, the unqualified call fails:
#include <algorithm>
int main() {
int a[5] = {3, 1, 4, 1, 5};
auto it = max_element(a, a + 5);
return *it == 5 ? 0 : 1;
}
-std=c++17 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:4:15: error: 'max_element' was not declared in this scope; did you mean 'std::max_element'?
4 | auto it = max_element(a, a + 5);
| ^~~~~~~~~~~
| std::max_element
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:528:1: note: 'std::max_element' declared here
528 | max_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
| ^~~~~~~~~~~
That wording - was not declared in this scope, with did you mean 'std::max_element'? on the error line and a note pointing at a declaration - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::max_element; adding includes does not bring the name out of namespace std. Swap the arrays for a vector, and the same unqualified call compiles and runs:
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5};
auto it = max_element(v.begin(), v.end());
return *it == 5 ? 0 : 1;
}
-std=c++17 g_unqualified_vec.cpp -o g_unqualified_vec_prog
$ ./g_unqualified_vec_prog
That compile is argument-dependent lookup at work, not a rule to rely on: the vector's iterators are std types, so their associated namespace is std, and lookup finds std::max_element through them. A list's iterators rescue it the same way - the same unqualified call over a list compiles and runs:
#include <algorithm>
#include <list>
int main() {
std::list<int> l = {3, 1, 4, 1, 5};
auto it = max_element(l.begin(), l.end());
return *it == 5 ? 0 : 1;
}
-std=c++17 g_unqualified_list.cpp -o g_unqualified_list_prog
$ ./g_unqualified_list_prog
But the container being a std type is not enough by itself - over a std::array, the same unqualified call fails with the same wording, because in this library a std::array's iterators are plain pointers, and a pointer has no associated namespace:
#include <algorithm>
#include <array>
int main() {
std::array<int, 5> a = {3, 1, 4, 1, 5};
auto it = max_element(a.begin(), a.end());
return *it == 5 ? 0 : 1;
}
-std=c++17 g_unqualified_stdarray.cpp -o g_unqualified_stdarray_prog
g_unqualified_stdarray.cpp: In function 'int main()':
g_unqualified_stdarray.cpp:5:15: error: 'max_element' was not declared in this scope; did you mean 'std::max_element'?
5 | auto it = max_element(a.begin(), a.end());
| ^~~~~~~~~~~
| std::max_element
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified_stdarray.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:528:1: note: 'std::max_element' declared here
528 | max_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
| ^~~~~~~~~~~
An initializer list's iterators fail the same way, for the same reason - in this library they are plain pointers too:
#include <algorithm>
#include <initializer_list>
int main() {
std::initializer_list<int> il = {3, 1, 4, 1, 5};
auto it = max_element(il.begin(), il.end());
return *it == 5 ? 0 : 1;
}
-std=c++17 g_unqualified_il.cpp -o g_unqualified_il_prog
g_unqualified_il.cpp: In function 'int main()':
g_unqualified_il.cpp:5:15: error: 'max_element' was not declared in this scope; did you mean 'std::max_element'?
5 | auto it = max_element(il.begin(), il.end());
| ^~~~~~~~~~~
| std::max_element
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified_il.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:528:1: note: 'std::max_element' declared here
528 | max_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
| ^~~~~~~~~~~
The pattern across the probed set is exact: the rescue follows the iterator type, not the container's name - class iterators of std containers carry the name in, pointer iterators do not, whatever container stands behind them. Write the qualifier either way.
The exact rule
std::max_elementis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here, 101 of the other 105 headers in the library's header set - among them<vector>,<iostream>,<iterator>,<numeric>,<ranges>, and<limits>- do not declare it, and neither does a file with no include at all; the remaining 4 pull it in transitively, as this library version's behavior, not as a guarantee.'max_element' is not a member of 'std'has one cause. The missing-include cause: the<algorithm>include is missing and nothing else in the file pulled the name in - in the sweep probed here, every failing sole header fails with one error line and no note lines and no help lines, in exactly two shapes: 56 headers fail with the plain error line alone, and 45 fail with the same line carrying the suggestion tail; did you mean 'tuple_element'?, and no probed sole header produces a note class or a declared-but-not-defined link-failure shape for this name. There is no version cause for the classic call: with the include present, the call compiles and runs as C++98.- The suggestion tail is not guidance:
std::tuple_elementis a type trait from<tuple>, not a function, and it appears only because the failing file's other includes make that name visible - in the probed set, exactly the 45 failing headers whose contents exposestd::tuple_elementproduce it, and the 56 whose contents do not, fail plainly. - In the sweep probed here, 4 headers besides
<algorithm>rescuestd::max_elementtransitively -<execution>,<functional>,<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, no header besides<algorithm>is required to pullstd::max_elementin. - The result rules are the documented ones, as the fixed programs here show: the fixed program prints
max = 5 at index 4for the five-element vector; the semantics program printsvector 9andlist 5,first-tie 9 at index 1for the first of two equally greatest elements,abs -14for the greatest by absolute value, andshortest figfor the greatest under the reversed size comparison. The call returns a position in the range passed, not the element itself. - The edge ranges are asymmetric: an empty range returns the last iterator - the edge program here prints
empty end- and a one-element range returns its only element - the edge program printsone 8. The empty result is the one that must not be dereferenced. - The classic
std::max_elementpredates C++11: with<algorithm>included it compiles and runs as C++98 and as C++11, reporting5both times for the largest element of five. The layered pieces have floors: constant-expression use and the execution-policy overloads both start at C++17 - with only<algorithm>included, namingstd::execution::seqfails with'std::execution' has not been declared; with<execution>included alongside it, the policy call compiles and runs; and compiled as C++14, the same program fails on the policy's name. - The iterators must meet the forward-iterator requirements. The forward-list program here shows the floor in action: over a forward list, whose iterators step in one direction only, the call prints
5. std::min_elementandstd::minmax_elementare declared by#include <algorithm>as well: in the programs here, a min_element call in a file whose only include is<vector>fails with the tuple_element suggestion and a minmax_element call in the same file fails plainly, and the fixed neighbor program, which includes both<algorithm>and<vector>, printsmin 1,max 9, andminmax 1 9. For none of the three names is the container's header the declaring header.std::ranges::max_elementis a ranges form of this same name in the same header: a function object in namespacestd::ranges, declared in<algorithm>, needing C++20 - compiled as C++17 the ranges program here fails with'std::ranges' has not been declared, and compiled as C++20 it prints9for the seven-element vector - and in the probes here<ranges>declares neither form: it fails plainly for the classic name in the sweep, and with<array>alongside it fails for the ranges form with'max_element' is not a member of 'std::ranges'.'max_element' was not declared in this scopefor an unqualifiedmax_elementuse is a different defect: qualify the name asstd::max_element- the include alone does not bring the name out of namespacestd. For this name the unqualified call's fate depends on the iterators' types: over plain arrays it fails, as the first unqualified program above shows; over a vector's iterators and over a list's iterators it is rescued through argument-dependent lookup, as the second and third show, because those iterators arestdclass types; over astd::array's iterators and over an initializer list's iterators it fails again, because in this library those iterators are plain pointers. Qualify the name either way.
The fixes, ranked
- First, check the include list of the file that names
std::max_element. If<algorithm>is not there, add it - that is the fix. It holds whether the file failed plainly or with the tuple_element suggestion, and whether it failed in a lean context or happened to compile before through one of the 4 transitive rescues and stopped compiling when its other includes changed. - Second, do not follow the diagnostic's suggestion:
tuple_elementis a type trait, not a function, and no edit toward it finds a largest element. The suggestion records which other names the file's includes made visible, nothing more. - Third, do not chase the standard flag for the classic call: with
<algorithm>included, it compiles and runs as C++98, so a too-old standard is not a cause of this error - if the include is present and the error persists, the include is in a different file than the one naming the call, or the name is misspelled. The flag matters only for the pieces layered on top: C++17 for constant-expression use and the execution-policy overloads, C++20 for the ranges form. - Fourth, do not reach for a container's header because the call walks a container, for
<iterator>because the call takes iterators, or for<numeric>because the call reduces a range to one answer. In the sweep probed here all three fail for this name - the declaring header is<algorithm>, and a complete program names it alongside the container's own header. - Fifth, do not remove
<algorithm>because a file compiles without it: in the probed set only 4 headers rescue it, and those rescues are not guaranteed - a file that drops the include is one internal-include change away from the error in the first section. - Sixth, if the error names
std::executioninstead -'std::execution' has not been declared- add<execution>alongside<algorithm>and keep the standard at C++17 or later: this name does have policy overloads, and that pair of changes is the whole fix for the policy form. - Seventh, match the standard to the one piece beyond the classic call that has a floor: constant-expression use and the policy forms need C++17, and the ranges form
std::ranges::max_elementneeds C++20 - with its header still<algorithm>, not<ranges>. - Eighth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::max_elementas well - and do not rely on argument-dependent lookup to rescue the unqualified call, because a plain array, astd::array, and an initializer list each remove that rescue in the probed set. - Ninth, if the failing name is a neighboring one, the fix is the same header:
min_elementandminmax_elementtake an added<algorithm>too - one header for all three extrema operations, and the container's header for the container alone.
Which fix for which cause
#include <algorithm>missing,std::max_elementused in a file whose only include is one of the 56 headers listed as plain-failing in the sweep section above, or that includes nothing: the message is'max_element' is not a member of 'std', with no suggestion tail, no note lines, and no help lines - add#include <algorithm>.#include <algorithm>missing,std::max_elementused in a file whose only include is one of the 45 headers listed as suggestion-failing in the sweep section above -<vector>,<iostream>, and<string>among them: the message is'max_element' is not a member of 'std'; did you mean 'tuple_element'?- add#include <algorithm>, and ignore the suggestion.std::max_elementused in a file whose only include is one of the 4 transitive-rescue headers listed in the sweep section above -<execution>,<functional>,<regex>,<valarray>: no error on this library version - add#include <algorithm>anyway, because those rescues are not guaranteed.std::max_elementused in a textbook file including<iostream>and<vector>: the error above, with the tuple_element suggestion - add#include <algorithm>; the textbook includes do not rescue this name.std::max_elementused with<algorithm>included, at any standard from C++98 up: no version error - the classic call predates C++11, and the same file reports5compiled as C++98 and as C++11.std::max_elementcalled with an execution policy, with<algorithm>included but<execution>not: the message is'std::execution' has not been declared- add<execution>; the policy overloads exist for this name since C++17, and compiled as C++14 the same program fails on the policy's name for the standard's reason.max_elementused without thestd::qualifier,<algorithm>included, over plain-array arguments, astd::array's iterators, or an initializer list's iterators: the message is'max_element' was not declared in this scope; did you mean 'std::max_element'?- qualify the name asstd::max_element. Over a vector's iterators or a list's iterators, the same unqualified call compiles through argument-dependent lookup - qualify it anyway, so the call does not depend on the iterators' types.
Checklist
- Check the include list of the file that names the call, not just the project. The include must be in that file, or in a header that file includes directly by design.
- Do not check the standard flag for the classic call: this is one of the names where the flag is not a cause - C++98 is old enough for it, and only the layered pieces have floors, C++17 for constant-expression use and the execution-policy overloads and C++20 for the ranges form.
- Do not import the container's header, the iterator header, or the numeric header into this error as the fix:
std::max_elementand its two neighbors take<algorithm>andstd::vectortakes<vector>, so there are two headers in a complete extrema program, one for the extrema operations and one for the container. - Do not assume the textbook includes rescue this name by rule, and do not delete a working
<algorithm>line because a file compiles without it. In the sweep here<iostream>and<vector>fail forstd::max_element- with a misleading suggestion attached - and the 4 headers that rescue it do so only as this library version's behavior. - Read the return value as a position, not a value: it is an iterator to the first of the equally greatest elements, it is the last iterator for an empty range - which must not be dereferenced - and under a comparator it is the position of whatever the comparator orders last. The fixed programs here print
max = 5 at index 4for the textbook case, andvector 9,list 5,first-tie 9 at index 1,abs -14, andshortest figfor the semantics cases. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
5under C++98 and C++11,5for the forward list,9for the ranges form at C++20, andmin 1,max 9, andminmax 1 9for the neighbor calls.
Related guides
- 'std::distance' Is Not a Member of 'std' Fix - the opposite rescue shape in the same family:
std::distancecounts the hops between two iterators, from<iterator>, a header 44 others pull in on the side, so its error hides until a lean file - where this guide's name, from<algorithm>, fails in almost every file that was never given the header, textbook files included. - 'std::count' Is Not a Member of 'std' Fix - the other scan-shaped question in this header:
std::counttallies the elements of a range that equal a value, from<algorithm>like this guide's name, wherestd::max_elementreturns an iterator to the range's largest element - two range scans whose home header is the same, and different from the container's.
Sources
- cppreference,
std::max_element— forstd::max_elementbeing a function template defined in<algorithm>; for the return being an iterator to the greatest element in the range, the first of them if several are equally greatest, and the last iterator if the range is empty; for the iterators needing to meet the forward-iterator requirements; for the classic form making exactly max(N-1, 0) comparisons over a range of N elements; for the function being usable in constant expressions since C++17; for the execution-policy overloads existing since C++17; and for defect report LWG 212, applied to C++98, settling that an empty range returns the last iterator. - cppreference, Standard library header
<algorithm>— for<algorithm>being the header whose synopsis declaresstd::max_element,std::min_element,std::minmax_element, and the C++20std::ranges::max_element, the home-header fact the fix, the neighbor, and the ranges sections of this guide rest on.
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.