C++
'minmax_element' Is Not a Member of 'std' Fix
std::minmax_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>. Its error is also the quietest in its family: where its neighbors' diagnostics at least offer a misleading suggestion, this name's missing-include error is one plain line with nothing after it - and the same wording hides a second cause its neighbors do not have, because this name starts at C++11. This guide pins down the failing shapes, the 4 transitive rescues, the pair the fixed call gives back, and the include - and the standard floor - that fix it by rule.
Written December 2, 2026 · Tested with GCC 13.3.0 · 29 min read
- The error: the include is missing
- The textbook version fails too - and its diagnostic offers nothing at all
- Neighboring headers: 101 fail in one shape, and 4 rescue it by accident
- The fix
Written December 2, 2026
Tested with GCC 13.3.0
What this guide is
std::minmax_element is a function template defined in the header <algorithm>. It returns a pair of iterators, to the smallest and the greatest element in a range, found in one pass: if several elements are equally smallest, the first member of the pair is the iterator to the first of them; if several are equally greatest, the second member is the iterator to the last of them; and if the range is empty, both members are the last iterator. The iterators need only meet the forward-iterator requirements, the classic form makes at most max(floor(3/2(N-1)), 0) comparisons over a range of N elements, and the function exists since 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> - 'minmax_element' is not a member of 'std' is usually the missing include, and naming that cause 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.
But this name, alone among its extrema neighbors, has a second cause with the same wording. std::minmax_element starts at C++11, so a file that includes <algorithm> and is compiled as C++98 reaches the same error line for the standard's reason, as the versions section below shows. The pieces layered on top have higher floors still - constant-expression use and the execution-policy overloads both need C++17 - so for this name a reader may have to fix the flag as well as the include, and the diagnostic cannot separate the two causes because it prints the same line for both.
A smallest-and-largest 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 one way - the failure, in a single diagnostic shape, 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 smallest and largest elements of a five-element array, but includes only <limits>:
#include <limits>
int main() {
int a[5] = {3, 1, 4, 1, 5};
auto p = std::minmax_element(a, a + 5);
return *p.first == 1 && *p.second == 5 ? 0 : 1;
}
-std=c++17 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:19: error: 'minmax_element' is not a member of 'std'
4 | auto p = std::minmax_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:19: error: 'minmax_element' is not a member of 'std'
3 | auto p = std::minmax_element(a, a + 5);
| ^~~~~~~~~~~~~~
The textbook version fails too - and its diagnostic offers nothing at all
The version most often written first - print the smallest and largest elements of a vector and where they sit - produces this error as well, without <algorithm>:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5};
auto p = std::minmax_element(v.begin(), v.end());
std::cout << "min = " << *p.first << " at index " << (p.first - v.begin())
<< ", max = " << *p.second << " at index " << (p.second - 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:19: error: 'minmax_element' is not a member of 'std'
5 | auto p = std::minmax_element(v.begin(), v.end());
| ^~~~~~~~~~~~~~
It fails, and the failure carries no suggestion. That silence is this name's own shape: its neighbors std::min_element and std::max_element, failing in the same kind of file, each carry a misleading did you mean 'tuple_element'? tail, as the neighboring-names section below shows - but no probed context gives minmax_element a suggestion tail on its missing-include error at all, not the textbook file, not any sole header in the sweep. There is nothing in the line to follow, wrong or right; the only information it carries is that the name is not there. A sole <vector> file fails with the same plain line:
-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:19: error: 'minmax_element' is not a member of 'std'
4 | auto p = std::minmax_element(v.begin(), v.end());
| ^~~~~~~~~~~~~~
A program that uses std::minmax_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 one shape, 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 smallest and largest elements of a five-element array and checks the result against 1 and 5, the outcome splits by compile result two ways - the failure, or a rescue - and the failure itself comes in exactly one diagnostic shape. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 101 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:
<any><array><atomic><barrier><bit><bitset><cassert><ccomplex><cctype><cerrno><cfenv><cfloat><charconv><chrono><cinttypes><ciso646><climits><clocale><cmath><codecvt><compare><complex><concepts><condition_variable><coroutine><csetjmp><csignal><cstdalign><cstdarg><cstdbool><cstddef><cstdint><cstdio><cstdlib><cstring><ctgmath><ctime><cuchar><cwchar><cwctype><deque><exception><expected><filesystem><format><forward_list><fstream><future><initializer_list><iomanip><ios><iosfwd><iostream><istream><iterator><latch><limits><list><locale><map><memory><memory_resource><mutex><new><numbers><numeric><optional><ostream><queue><random><ranges><ratio><scoped_allocator><semaphore><set><shared_mutex><source_location><span><spanstream><sstream><stack><stacktrace><stdexcept><stdfloat><stop_token><streambuf><string><string_view><syncstream><system_error><thread><tuple><type_traits><typeindex><typeinfo><unordered_map><unordered_set><utility><variant><vector><version>
A file with no include at all fails the same plain way. Three entries in this list deserve a pause. <iterator> fails: the header of the iterator operations does not declare this algorithm, however iterator-shaped its arguments look. <numeric> fails: the header of the accumulating names does not declare this one either. And <ranges> fails: the C++20 ranges library has its own minmax_element name, and it does not bring this one in - that name gets its own section below. The contrast with the neighboring names is exact: for std::max_element, 45 of the failing headers carried the tuple_element suggestion, because that shorter name sits close enough in spelling to a name those headers expose; minmax_element is a longer, rarer spelling, no failing header exposes a near enough name, and so every one of the 101 failures is bare.
And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><execution><functional><regex><valarray>
The rescue set is the same 5 entries as the neighboring extrema names, and the shape is the same: <algorithm> sits on top of the container and stream headers, almost nothing pulls it in on the side, and the four headers that do are headers few smallest-and-largest 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 p = std::minmax_element(v.begin(), v.end());
std::cout << "min = " << *p.first << " at index " << (p.first - v.begin())
<< ", max = " << *p.second << " at index " << (p.second - v.begin()) << "\n";
return 0;
}
-std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
min = 1 at index 1, max = 5 at index 4
One include, <algorithm>, declares the function - and the textbook program, which failed outright before, now prints min = 1 at index 1, max = 5 at index 4: the smallest element of 3, 1, 4, 1, 5 is the 1, sitting at index 1, and the largest is the 5, sitting at index 4, both found by the one call. 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: a pair of iterators, first smallest and last of the greatest
The call returns a pair of iterators, not two values, and which elements the pair lands on follows the range. Over a vector the pair lands on the smallest and greatest elements; over a list it does the same by stepping; and when the extreme values occur twice the two ends of the pair follow different rules - the smallest end takes the first occurrence, the greatest end takes the last:
#include <algorithm>
#include <iostream>
#include <list>
#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 = {1, 9, 4, 9, 1};
auto pv = std::minmax_element(v.begin(), v.end());
std::cout << "vector " << *pv.first << " " << *pv.second << "\n";
auto pl = std::minmax_element(l.begin(), l.end());
std::cout << "list " << *pl.first << " " << *pl.second << "\n";
auto pt = std::minmax_element(ties.begin(), ties.end());
std::cout << "ties min " << *pt.first << " at index " << (pt.first - ties.begin())
<< ", max " << *pt.second << " at index " << (pt.second - ties.begin()) << "\n";
return 0;
}
-std=c++17 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
vector 1 9
list 1 5
ties min 1 at index 0, max 9 at index 3
Read in order: the vector line reports 1 9, the smallest and largest of its seven elements, and the list line reports 1 5 - the same kind of answer over a range whose iterators can only be stepped one at a time. The ties line reports min 1 at index 0, max 9 at index 3 over 1, 9, 4, 9, 1: the value 1 occurs at indices 0 and 4, and the pair's first member lands on the first of the equally smallest elements; the value 9 occurs at indices 1 and 3, and the pair's second member lands on the last of the equally greatest elements. That asymmetry is this function's own rule, and it differs from its neighbors used separately: std::max_element on the same range returns the first 9, at index 1, so std::minmax_element is not interchangeable with a pair of single-end calls when ties matter. In every case the call returns two positions in the range passed, and dereferencing each gives the element at that position.
Comparators: one comparison orders both ends
A comparator redefines what counts as smallest and greatest at the same time: the first member of the pair is the element the comparator orders first, and the second member is the element it orders last:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<int> vals = {-3, 1, -14, 1, 5};
std::vector<std::string> words = {"pear", "fig", "banana"};
auto a = std::minmax_element(vals.begin(), vals.end(),
[](int x, int y) { return std::abs(x) < std::abs(y); });
std::cout << "abs min " << *a.first << ", max " << *a.second << "\n";
auto s = std::minmax_element(words.begin(), words.end(),
[](const std::string& x, const std::string& y) { return x.size() > y.size(); });
std::cout << "reversed-size min " << *s.first << ", max " << *s.second << "\n";
return 0;
}
-std=c++17 r_compare.cpp -o r_compare_prog
$ ./r_compare_prog
abs min 1, max -14
reversed-size min banana, max fig
The abs line reports min 1, max -14: with a comparator that orders by absolute value, the smallest element is the one with the least absolute value - the first 1 - and the greatest is the one with the greatest absolute value, -14, with the iterator landing on the element itself, sign included. The reversed-size line reports min banana, max fig: the comparator given there orders longer strings as smaller, so the element the call treats as smallest is the longest string, banana, and the element it treats as greatest is the shortest, fig. One comparison function drives both ends of the pair, so a reversed ordering flips the two ends together.
The edge ranges: empty and one element
No short range is an error. An empty range returns the last iterator in both members of the pair - there is no smallest or greatest element to point at - and a one-element range returns its only element at both ends:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> e;
std::vector<int> one = {8};
auto pe = std::minmax_element(e.begin(), e.end());
std::cout << "empty " << (pe.first == e.end() && pe.second == e.end() ? "end end" : "value") << "\n";
auto po = std::minmax_element(one.begin(), one.end());
std::cout << "one " << *po.first << " " << *po.second << "\n";
return 0;
}
-std=c++17 k_empty.cpp -o k_empty_prog
$ ./k_empty_prog
empty end end
one 8 8
The empty call reports end end because both members hold the past-the-end iterator: the documented result for an empty range is a pair of last iterators, so a caller that dereferences either member without checking has to rule the empty case out first. The one-element call reports 8 8 - the single element is trivially both the smallest and 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 smallest and largest elements 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};
auto p = std::minmax_element(fl.begin(), fl.end());
std::cout << *p.first << " " << *p.second << "\n";
return 0;
}
-std=c++17 f_forward.cpp -o f_forward_prog
$ ./f_forward_prog
1 5
It reports 1 5. The scan is a single forward pass - the classic form makes at most three comparisons for every two elements after the first pair - and a forward-only range is enough for it, which is the floor the list line in the return-value section also stands on.
Versions: this name starts at C++11 - a second cause its neighbors do not have
This call needs a modern-enough standard, and that is what separates it from its neighbors. std::min_element and std::max_element predate C++11; std::minmax_element starts at C++11. The fixed programs in this guide are compiled as C++17, but the floor is lower: with <algorithm> included, the same call compiled as C++11 compiles and runs - and compiled as C++98 it fails, with the very error this guide is named for:
#include <algorithm>
#include <iostream>
#include <utility>
int main() {
int a[5] = {3, 1, 4, 1, 5};
std::pair<int*, int*> p = std::minmax_element(a, a + 5);
std::cout << *p.first << " " << *p.second << "\n";
return 0;
}
-std=c++98 c_version98.cpp -o c_version98_prog
c_version98.cpp: In function 'int main()':
c_version98.cpp:6:36: error: 'minmax_element' is not a member of 'std'; did you mean 'min_element'?
6 | std::pair<int*, int*> p = std::minmax_element(a, a + 5);
| ^~~~~~~~~~~~~~
| min_element
-std=c++11 c_version11.cpp -o c_version11_prog
$ ./c_version11_prog
1 5
The C++11 build prints 1 5; the C++98 build prints the familiar error line. Two details of the C++98 failure matter. First, its wording is the missing-include wording - 'minmax_element' is not a member of 'std' - even though the include is present in that file, so the diagnostic alone cannot tell a reader which of the two causes they have. Second, this is the one context where this name's error does carry a suggestion: did you mean 'min_element'? - and it is a trap of a quieter kind than the neighbors' tuple_element tail, because std::min_element is a real function that exists at C++98 and would compile. Following it fixes the error by changing the program: it finds only the smallest element, and the greatest element the program wanted is silently gone. And the two causes do not substitute for each other: compiled as C++11 with the include still missing, the same program fails again, plainly:
-std=c++11 c_version11_noinclude.cpp -o c_version11_noinclude_prog
c_version11_noinclude.cpp: In function 'int main()':
c_version11_noinclude.cpp:5:36: error: 'minmax_element' is not a member of 'std'
5 | std::pair<int*, int*> p = std::minmax_element(a, a + 5);
| ^~~~~~~~~~~~~~
So this name's error has two causes with two fixes - add <algorithm>, and compile as C++11 or later - and a file can need either or both. Unlike its single-end neighbors, 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 p = std::minmax_element(std::execution::seq, v.begin(), v.end());
return *p.first == 1 && *p.second == 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:39: error: 'std::execution' has not been declared
5 | auto p = std::minmax_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::minmax_element exist:
#include <algorithm>
#include <execution>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5};
auto p = std::minmax_element(std::execution::seq, v.begin(), v.end());
return *p.first == 1 && *p.second == 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:39: error: 'std::execution' has not been declared
6 | auto p = std::minmax_element(std::execution::seq, v.begin(), v.end());
| ^~~~~~~~~
And since C++17 the classic call is also usable in constant expressions - this program's smallest and largest elements are found at compile time, and the assertion on them is part of the build:
#include <algorithm>
#include <array>
constexpr bool check() {
std::array<int, 5> a = {3, 1, 4, 1, 5};
auto p = std::minmax_element(a.begin(), a.end());
return *p.first == 1 && *p.second == 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, C++11 is the floor for the classic call, and no standard at any level rescues a missing include.
The neighboring names: min_element and max_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::max_element returns an iterator to the largest - and both are declared by <algorithm>, alongside std::minmax_element. They share this name's failure as well, but not its silence: in a file whose only include is <vector>, a std::min_element call fails with a suggestion tail:
-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::max_elementcall in the same file fails with the same suggestion:
-std=c++17 b_max_vector.cpp -o b_max_vector_prog
b_max_vector.cpp: In function 'int main()':
b_max_vector.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
The suggestion is tuple_element - a type trait from <tuple> that names the type of one element of a tuple-like object; it is not a function, it finds nothing, and following it leads away from the fix for either neighbour. It appears because <vector> makes tuple_element visible in namespace std, and the compiler offers the nearest visible name it can find - nearest in spelling, not in meaning. This guide's name, failing in the very same file, gets no such tail, as the textbook section shows: the neighbors' diagnostics mislead, and this name's diagnostic merely says nothing. 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 the two single-end calls report separately. 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::minmax_element is a function object in the ranges namespace, declared in <algorithm>, that also accepts a whole range, and it returns a result object whose members are named min and max rather than a pair's first and second. Compiled as C++20 it reports 1 9 for the same seven-element vector the return-value program scans:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2};
auto r = std::ranges::minmax_element(v);
std::cout << *r.min << " " << *r.max << "\n";
return 0;
}
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
1 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:27: error: 'minmax_element' is not a member of 'std::ranges'
5 | auto r = std::ranges::minmax_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::minmax_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 p = minmax_element(a, a + 5);
return *p.first == 1 && *p.second == 5 ? 0 : 1;
}
-std=c++17 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:4:14: error: 'minmax_element' was not declared in this scope; did you mean 'std::minmax_element'?
4 | auto p = minmax_element(a, a + 5);
| ^~~~~~~~~~~~~~
| std::minmax_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:536:1: note: 'std::minmax_element' declared here
536 | minmax_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
| ^~~~~~~~~~~~~~
That wording - was not declared in this scope, with did you mean 'std::minmax_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::minmax_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 p = minmax_element(v.begin(), v.end());
return *p.first == 1 && *p.second == 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::minmax_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 p = minmax_element(l.begin(), l.end());
return *p.first == 1 && *p.second == 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 p = minmax_element(a.begin(), a.end());
return *p.first == 1 && *p.second == 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:14: error: 'minmax_element' was not declared in this scope; did you mean 'std::minmax_element'?
5 | auto p = minmax_element(a.begin(), a.end());
| ^~~~~~~~~~~~~~
| std::minmax_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:536:1: note: 'std::minmax_element' declared here
536 | minmax_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 p = minmax_element(il.begin(), il.end());
return *p.first == 1 && *p.second == 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:14: error: 'minmax_element' was not declared in this scope; did you mean 'std::minmax_element'?
5 | auto p = minmax_element(il.begin(), il.end());
| ^~~~~~~~~~~~~~
| std::minmax_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:536:1: note: 'std::minmax_element' declared here
536 | minmax_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::minmax_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.'minmax_element' is not a member of 'std'has two causes, and the diagnostic cannot separate them because both print the same error line. 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 suggestion tail, no note lines, and no help lines, all 101 in the one plain shape. The version cause: the include is present but the file is compiled as C++98, before this name existed - that failure carries the error line's one suggestion in this guide,; did you mean 'min_element'?. No standard flag rescues a missing include, and no include rescues a pre-C++11 standard.- The C++98 suggestion is not guidance either:
std::min_elementis a real neighboring function, and switching to it would compile - by dropping the greatest-element half of what this call returns. The suggestion records which names the standard in use makes available, nothing more. - In the sweep probed here, 4 headers besides
<algorithm>rescuestd::minmax_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::minmax_elementin. - The result rules are the documented ones, as the fixed programs here show: the fixed program prints
min = 1 at index 1, max = 5 at index 4for the five-element vector; the return-value program printsvector 1 9andlist 1 5, andties min 1 at index 0, max 9 at index 3- the first of two equally smallest elements at the pair's first end, the last of two equally greatest at its second end. The call returns two positions in the range passed, not the elements themselves. - The edge ranges are symmetric, unlike the single-end names: an empty range returns the last iterator in both members - the edge program here prints
empty end end- and a one-element range returns its only element at both ends - the edge program printsone 8 8. Either member of the empty result must not be dereferenced. - The classic
std::minmax_elementstarts at C++11: with<algorithm>included it compiles and runs as C++11, reporting1 5for the smallest and largest of five, and fails as C++98 with the same error wording. The layered pieces have higher 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
1 5. std::min_elementandstd::max_elementare declared by#include <algorithm>as well: in the programs here, a min_element call and a max_element call in a file whose only include is<vector>each fail with the tuple_element suggestion that this guide's name never gets, 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::minmax_elementis a ranges form of this same name in the same header: a function object in namespacestd::ranges, declared in<algorithm>, needing C++20, returning a result whose members are namedminandmax- compiled as C++17 the ranges program here fails with'std::ranges' has not been declared, and compiled as C++20 it prints1 9for 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'minmax_element' is not a member of 'std::ranges'.'minmax_element' was not declared in this scopefor an unqualifiedminmax_elementuse is a different defect: qualify the name asstd::minmax_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::minmax_element. If<algorithm>is not there, add it - that is the fix for the missing-include cause. It holds whether the file 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, check the standard the file is compiled as. This name starts at C++11, so if
<algorithm>is present and the error persists, a pre-C++11 standard is the remaining cause - raise it to C++11 or later. Neither fix substitutes for the other: the include fails at C++11 when missing, and the name fails at C++98 when present. - Third, do not follow the one suggestion this name's error ever offers: at C++98 the diagnostic suggests
min_element, a real function that would compile - and would silently drop the greatest element the program asked this call for. Raise the standard instead of narrowing the program. - 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 two answers. 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::minmax_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::minmax_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_elementandmax_elementtake an added<algorithm>too - one header for all three extrema operations, and the container's header for the container alone. Their pre-C++11 history differs from this name's, but the include fix is identical.
Which fix for which cause
#include <algorithm>missing,std::minmax_elementused in a file whose only include is one of the 101 headers listed as plain-failing in the sweep section above, or that includes nothing: the message is'minmax_element' is not a member of 'std', with no suggestion tail, no note lines, and no help lines - add#include <algorithm>.std::minmax_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::minmax_elementused in a textbook file including<iostream>and<vector>: the error above, plain, with no suggestion - add#include <algorithm>; the textbook includes do not rescue this name.std::minmax_elementused with<algorithm>included, compiled as C++98: the message is'minmax_element' is not a member of 'std'; did you mean 'min_element'?- raise the standard to C++11 or later, and do not switch the call to the suggested name.std::minmax_elementused with<algorithm>included, compiled as C++11 or later: no version error - the classic call starts at C++11, and the same file reports1 5compiled as C++11.std::minmax_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.minmax_elementused without thestd::qualifier,<algorithm>included, over plain-array arguments, astd::array's iterators, or an initializer list's iterators: the message is'minmax_element' was not declared in this scope; did you mean 'std::minmax_element'?- qualify the name asstd::minmax_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.
- Check the standard flag for this name: unlike its single-end neighbors, this call has a floor - C++11 for the classic call, C++17 for constant-expression use and the execution-policy overloads, and C++20 for the ranges form. A file can need the flag fix, the include fix, or both, and the error line does not say which.
- Do not import the container's header, the iterator header, or the numeric header into this error as the fix:
std::minmax_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::minmax_element- plainly, with no suggestion attached - and the 4 headers that rescue it do so only as this library version's behavior. - Read the return value as two positions, not two values: it is a pair whose first member is the iterator to the first of the equally smallest elements and whose second member is the iterator to the last of the equally greatest elements; for an empty range both members are the last iterator, which must not be dereferenced; and under a comparator the two ends are whatever the comparator orders first and last. The fixed programs here print
min = 1 at index 1, max = 5 at index 4for the textbook case, andvector 1 9,list 1 5, andties min 1 at index 0, max 9 at index 3for the return-value cases. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
1 5under C++11,1 5for the forward list,1 9for the ranges form at C++20, andmin 1,max 9, andminmax 1 9for the neighbor calls.
Related guides
- 'std::max_element' Is Not a Member of 'std' Fix - the single-end half of this guide's pair:
std::max_elementreturns an iterator to a range's largest element, from<algorithm>like this guide's name, wherestd::minmax_elementreturns both ends in one pass - and the two differ on ties, this name taking the last of the equally greatest elements wherestd::max_elementtakes the first, and on history, this name starting at C++11 where the single-end names predate it. - '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.
Sources
- cppreference,
std::minmax_element— forstd::minmax_elementbeing a function template defined in<algorithm>; for the return being a pair whose first member is an iterator to the smallest element, the first of them if several are equally smallest, and whose second member is an iterator to the greatest element, the last of them if several are equally greatest, with both members the last iterator if the range is empty; for the iterators needing to meet the forward-iterator requirements; for the classic form making at most max(floor(3/2(N-1)), 0) comparisons over a range of N elements; for the function existing since C++11 and being usable in constant expressions since C++17; and for the execution-policy overloads existing since C++17. - cppreference, Standard library header
<algorithm>— for<algorithm>being the header whose synopsis declaresstd::minmax_element,std::min_element,std::max_element, and the C++20std::ranges::minmax_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.