Cpp Std Min Element Not Member Fix | Veldica

layout: landing explore_theme: programming analytics_page: cpp_std_min_element_not_member_fix analytics_page_view_event: page_view_cpp_std_min_element_not_member_fix page_title: "'std::min_element' Is Not a Member of 'std' Fix" page_description: "A program that uses std::min_element fails with ''min_element' is not a member of 'std'' when the #include line is missing and nothing else in the file pulls the name in. On GCC 13.3.0 the outcome splits three ways in the sweep probed here - every one of the 106 standard headers in this library's header set compiled as the only header in the program, with every diagnostic component classified separately - the error line itself, any suggestion tail on that line, any note lines, and any help lines: 49 failing sole headers fail plainly, with no suggestion on the error line, no note lines, and no help lines, among them , , , , and ; 51 failing sole headers carry a suggestion tail, and it is the same wrong name behind every one of them - 'tuple_element', a class template that names an element type of a tuple-like type and cannot be called at all - among them , , , , , and , so this error does carry a suggestion tail where its sibling std::includes carries none, and the tail it carries is never the fix; and only five headers besides itself rescue the name transitively, , , , , and . No probed header produces a declared-but-not-defined link failure. The textbook version fails too, with the suggestion: a program including and that prints the smallest element of a vector gets the same error, with '; did you mean 'tuple_element'?' on the error line. This guide reproduces both failure shapes exactly as GCC 13.3.0 prints them in the C locale, gives the fix - add - and shows the fixed programs reporting 1 and 6 as the smallest elements of two vectors, reporting 1 four times for a hand-loop check of the answer, an empty range returning its end iterator, a one-element range, and the first of two tied smallest elements, running the comparator overload with std::greater to report 6 and 9, running a static_assert over the call in a constant expression, and running std::ranges::min_element. std::min_element does have execution-policy overloads, since C++17: passing std::execution::par with and included compiles and runs, reporting 1. The neighboring names std::max_element and std::minmax_element live in the same header and fail plainly, with no suggestion, in the lean context probed here, std::ranges::min_element fails as ''std::ranges' has not been declared' in that context and as ''min_element' is not a member of 'std::ranges'' behind a sole , and works with , and an unqualified min_element behaves two ways: with std::vector iterators as its arguments it compiles and runs through argument-dependent lookup, while with std::array iterators and with raw pointers it fails with 'min_element' was not declared in this scope instead." page_kicker: "C++" hero_title: "'std::min_element' Is Not a Member of 'std' Fix" hero_body: "std::min_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 <chrono>, <execution>, <functional>, <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 takes two failing shapes - the plain error, or the same error carrying a wrong-name suggestion, 'tuple_element', which names a type trait and cannot be called - or a transitive rescue. This guide pins down all three outcomes, which probed headers cause each one, and the one include that fixes the error by specification." slug: cpp-std-min-element-not-member-fix page_schema: "@context": https://schema.org "@type": Article author: "@type": Organization name: Veldica url: https://veldica.com/ datePublished: "2026-12-01" description: "A program that uses std::min_element fails with ''min_element' is not a member of 'std'' when the #include line is missing and nothing else in the file pulls the name in. On GCC 13.3.0 the outcome splits three ways in the sweep probed here - every one of the 106 standard headers in this library's header set compiled as the only header in the program, with every diagnostic component classified separately - the error line itself, any suggestion tail on that line, any note lines, and any help lines: 49 failing sole headers fail plainly, with no suggestion on the error line, no note lines, and no help lines, among them , , , , and ; 51 failing sole headers carry a suggestion tail, and it is the same wrong name behind every one of them - 'tuple_element', a class template that names an element type of a tuple-like type and cannot be called at all - among them , , , , , and , so this error does carry a suggestion tail where its sibling std::includes carries none, and the tail it carries is never the fix; and only five headers besides itself rescue the name transitively, , , , , and . No probed header produces a declared-but-not-defined link failure. The textbook version fails too, with the suggestion: a program including and that prints the smallest element of a vector gets the same error, with '; did you mean 'tuple_element'?' on the error line. This guide reproduces both failure shapes exactly as GCC 13.3.0 prints them in the C locale, gives the fix - add - and shows the fixed programs reporting 1 and 6 as the smallest elements of two vectors, reporting 1 four times for a hand-loop check of the answer, an empty range returning its end iterator, a one-element range, and the first of two tied smallest elements, running the comparator overload with std::greater to report 6 and 9, running a static_assert over the call in a constant expression, and running std::ranges::min_element. std::min_element does have execution-policy overloads, since C++17: passing std::execution::par with and included compiles and runs, reporting 1. The neighboring names std::max_element and std::minmax_element live in the same header and fail plainly, with no suggestion, in the lean context probed here, std::ranges::min_element fails as ''std::ranges' has not been declared' in that context and as ''min_element' is not a member of 'std::ranges'' behind a sole , and works with , and an unqualified min_element behaves two ways: with std::vector iterators as its arguments it compiles and runs through argument-dependent lookup, while with std::array iterators and with raw pointers it fails with 'min_element' was not declared in this scope instead." headline: "'std::min_element' Is Not a Member of 'std' Fix" mainEntityOfPage: "@id": https://veldica.com/guides/cpp-std-min-element-not-member-fix "@type": WebPage publisher: "@type": Organization name: Veldica url: https://veldica.com/


Written December 1, 2026

Tested with GCC 13.3.0

What this guide is

std::min_element is a function template defined in the header <algorithm>. It finds the smallest element in the range [first, last) and returns an iterator to it. With the plain overload the order is operator< (until C++20) / std::less{} (since C++20), and the comparator overload tests with respect to the comparison the program passes instead. If several elements are equally smallest, it returns the iterator to the first of them, and if the range is empty it returns last - a defect report against C++98 already clarifies that empty-range return. It makes exactly max(N-1, 0) comparisons, where N is the length of the range. Both plain overloads are constexpr since C++17, and std::min_element does have execution-policy overloads, since C++17 - the fixed-programs section below runs one of each. Finding the smaller of two values instead of the smallest of a range is a separate operation: std::min has its own guide and is not re-taught here, and std::max has its own guide too.

Because the template lives in one specific header, 'min_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 behind 51 of the failing sole headers it does add a suggestion tail, and the suggested name is always the same wrong one: tuple_element, a class template that names the type of an element of a tuple-like type. That name is not a function and cannot be called, so the tail is a dead end wherever it appears. The neighboring names probed here - std::max_element and std::minmax_element, both in the same header - get no suggestion at all in the 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. The older siblings in this family - std::sort, std::is_sorted, std::binary_search, std::partial_sort, std::partition, std::rotate, and std::includes - 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 returns success if the smallest element of an array is 1, but includes only <limits>:

#include <limits>
int main() {
    int a[] = {4, 2, 5, 1, 3, 6};
    return *std::min_element(a, a + 6) == 1 ? 0 : 1;
}
$ g++ -std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:18: error: 'min_element' is not a member of 'std'
    4 |     return *std::min_element(a, a + 6) == 1 ? 0 : 1;
      |                  ^~~~~~~~~~~

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[] = {4, 2, 5, 1, 3, 6};
    return *std::min_element(a, a + 6) == 1 ? 0 : 1;
}
$ g++ -std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:18: error: 'min_element' is not a member of 'std'
    3 |     return *std::min_element(a, a + 6) == 1 ? 0 : 1;
      |                  ^~~~~~~~~~~

The textbook version fails too - with a wrong-name suggestion

The version most often written first - print the smallest element of a vector, with std::cout - gets the same error, without <algorithm>, and here the error line does carry a tail:

#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {4, 2, 5, 1, 3, 6};
    std::cout << *std::min_element(v.begin(), v.end()) << "\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:24: error: 'min_element' is not a member of 'std'; did you mean 'tuple_element'?
    5 |     std::cout << *std::min_element(v.begin(), v.end()) << "\n";
      |                        ^~~~~~~~~~~
      |                        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 - and what those headers do pull in includes std::tuple_element, the class template that names an element type of a tuple, a pair, or an array, whose name is close enough to min_element for GCC to offer it as a suggestion. It is not a near miss worth following: tuple_element is a type trait, not a callable, and renaming the call to it cannot produce a working program. In the sweep probed here, the only headers that rescue std::min_element are <chrono>, <execution>, <functional>, <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::min_element includes <algorithm>, whether or not it happens to compile without it.

Neighboring headers: 49 fail plainly, 51 carry the suggestion, and five 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 returns success if the smallest element of an array is 1, the outcome splits by compile result 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>

And these 51 headers fail with the same error carrying the suggestion tail ; did you mean 'tuple_element'?, again 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 file with no include at all fails the plain way. No failing sole header prints note lines or help lines in either failing class.

A sole <vector> file shows the suggestion shape in full:

#include <vector>
int main() {
    int a[] = {4, 2, 5, 1, 3, 6};
    return *std::min_element(a, a + 6) == 1 ? 0 : 1;
}
$ g++ -std=c++20 v_vector.cpp -o v_vector_prog
v_vector.cpp: In function 'int main()':
v_vector.cpp:4:18: error: 'min_element' is not a member of 'std'; did you mean 'tuple_element'?
    4 |     return *std::min_element(a, a + 6) == 1 ? 0 : 1;
      |                  ^~~~~~~~~~~
      |                  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, 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 here is large but uniform in the wrong direction: where std::is_sorted's sweep carries the is_signed suggestion behind 66 headers and std::includes' sweep carries no suggestion at all, this sweep's 51 suggestion headers all name the same type trait, tuple_element - a name that shares a suffix with min_element and nothing else. <algorithm> - the home header - is on the rescue 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 = {4, 2, 5, 1, 3, 6};
    std::vector<int> w = {7, 9, 6, 8};
    std::cout << *std::min_element(v.begin(), v.end()) << " " << *std::min_element(w.begin(), w.end()) << "\n";
    return 0;
}
$ g++ -std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
1 6

One include, <algorithm>, declares the template: the smallest element of the first vector is 1 and the smallest element of the second is 6, so the call distinguishes the two ranges instead of answering 1 for everything.

The fixed programs: empty, single, and tied ranges, checked element by element

The answer leaves one thing to check independently: no element of the range is smaller than the one the call returns. This program runs that element-by-element search alongside std::min_element, and reports the empty-range, one-element, and tied-minimum answers beside it:

#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {4, 2, 5, 1, 3, 6};
    std::vector<int> e;
    std::vector<int> one = {9};
    std::vector<int> ties = {3, 1, 4, 1, 5};
    int m = v[0];
    for (int x : v) if (x < m) m = x;
    bool ok = (*std::min_element(v.begin(), v.end()) == m);
    bool empty_is_last = (std::min_element(e.begin(), e.end()) == e.end());
    bool single = (*std::min_element(one.begin(), one.end()) == 9);
    bool first_tie = (std::min_element(ties.begin(), ties.end()) == ties.begin() + 1);
    std::cout << ok << " " << empty_is_last << " " << single << " " << first_tie << "\n";
    return 0;
}
$ g++ -std=c++20 k_values.cpp -o k_values_prog
$ ./k_values_prog
1 1 1 1

Read in order: the element-by-element check confirms the smallest element, an empty range returns its end iterator (1), a one-element range returns that element (1), and where the smallest value 1 appears twice the call returns the first of the two positions (1).

The comparator overload tests the order the program passes instead: with std::greater<int>() from <functional>, the element that counts as smallest under that order is the largest value in the range, and the program prints 6 9 for its two ranges:

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {4, 2, 5, 1, 3, 6};
    std::vector<int> w = {7, 9, 8};
    std::cout << *std::min_element(v.begin(), v.end(), std::greater<int>()) << " " << *std::min_element(w.begin(), w.end(), std::greater<int>()) << "\n";
    return 0;
}
$ g++ -std=c++20 j_comp.cpp -o j_comp_prog
$ ./j_comp_prog
6 9

There is also an execution-policy version to run. Passing std::execution::par as a first argument selects the policy overload that has existed since C++17, and the call compiles and runs, printing 1 for the smallest element:

#include <algorithm>
#include <execution>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {4, 2, 5, 1, 3, 6};
    std::cout << *std::min_element(std::execution::par, v.begin(), v.end()) << "\n";
    return 0;
}
$ g++ -std=c++20 j_policy.cpp -o j_policy_prog
$ ./j_policy_prog
1

So a smallest-element search can take a policy; the policy overloads are part of this name's own overload set, and <execution> is the header the policy argument itself comes from.

And the call is usable in a constant expression. Both plain overloads are constexpr since C++17, so a static_assert over the dereferenced result compiles and the program runs with no output:

#include <algorithm>
#include <array>
int main() {
    constexpr std::array<int, 5> a = {4, 2, 5, 1, 3};
    static_assert(*std::min_element(a.begin(), a.end()) == 1);
    return 0;
}
$ g++ -std=c++20 c_constexpr.cpp -o c_constexpr_prog
$ ./c_constexpr_prog

The neighboring names: same header, plain in the lean context

The names that std::min_element is built alongside share its home header - the largest-element search and the combined search. In the lean context here - only <limits> included - they get no suggestion at all. std::max_element - the function template defined in <algorithm> that finds the largest element in a range - gets its own error, again with no header note. It is shown here only as a neighbor, not re-taught:

#include <limits>
int main() {
    int a[] = {4, 2, 5, 1, 3, 6};
    return *std::max_element(a, a + 6) == 6 ? 0 : 1;
}
$ g++ -std=c++20 b_max_element.cpp -o b_max_element_prog
b_max_element.cpp: In function 'int main()':
b_max_element.cpp:4:18: error: 'max_element' is not a member of 'std'
    4 |     return *std::max_element(a, a + 6) == 6 ? 0 : 1;
      |                  ^~~~~~~~~~~

std::minmax_element - the function template defined in <algorithm> that finds the smallest and the largest elements in a range in one pass - fails the same way:

#include <limits>
int main() {
    int a[] = {4, 2, 5, 1, 3, 6};
    auto p = std::minmax_element(a, a + 6);
    return *p.first == 1 && *p.second == 6 ? 0 : 1;
}
$ g++ -std=c++20 b_minmax_element.cpp -o b_minmax_element_prog
b_minmax_element.cpp: In function 'int main()':
b_minmax_element.cpp:4:19: error: 'minmax_element' is not a member of 'std'
    4 |     auto p = std::minmax_element(a, a + 6);
      |                   ^~~~~~~~~~~~~~

One contrast is worth holding on to: in the lean programs here, min_element, max_element, and minmax_element all get no note, and only min_element's wider sweep carries a suggestion tail at all - behind the headers that pull tuple_element in. Do not read either shape 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: the largest element is 6, and the combined search reports the pair 1 and 6, printing 6 1 6:

#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {4, 2, 5, 1, 3, 6};
    auto p = std::minmax_element(v.begin(), v.end());
    std::cout << *std::max_element(v.begin(), v.end()) << " " << *p.first << " " << *p.second << "\n";
    return 0;
}
$ g++ -std=c++20 k_neighbors_fixed.cpp -o k_neighbors_fixed_prog
$ ./k_neighbors_fixed_prog
6 1 6

Since C++20 the same <algorithm> header also declares std::ranges::min_element, the ranges form of the same search. It comes in two call shapes - an iterator pair, and a single range - and returns the iterator directly: here both shapes report the element 1 for the first vector and the single-range shape reports 6 for the second, printing 1 1 6. 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. Behind a sole <vector> the wording changes: the namespace exists there, but the name is missing from it, so the error is 'min_element' is not a member of 'std::ranges':

#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {4, 2, 5, 1, 3, 6};
    std::vector<int> w = {7, 9, 6, 8};
    std::cout << *std::ranges::min_element(v.begin(), v.end()) << " " << *std::ranges::min_element(v) << " " << *std::ranges::min_element(w) << "\n";
    return 0;
}
$ g++ -std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
1 1 6
#include <limits>
int main() {
    int a[] = {4, 2, 5, 1, 3, 6};
    return *std::ranges::min_element(a, a + 6) == 1 ? 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:18: error: 'std::ranges' has not been declared
    4 |     return *std::ranges::min_element(a, a + 6) == 1 ? 0 : 1;
      |                  ^~~~~~
#include <vector>
int main() {
    std::vector<int> v = {4, 2, 5, 1, 3, 6};
    return *std::ranges::min_element(v.begin(), v.end()) == 1 ? 0 : 1;
}
$ g++ -std=c++20 r3_ranges_vector.cpp -o r3_ranges_vector_prog
r3_ranges_vector.cpp: In function 'int main()':
r3_ranges_vector.cpp:4:26: error: 'min_element' is not a member of 'std::ranges'
    4 |     return *std::ranges::min_element(v.begin(), v.end()) == 1 ? 0 : 1;
      |                          ^~~~~~~~~~~

A different wording - and cases where the qualifier is not needed, and where it is

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 C arrays:

#include <algorithm>
int main() {
    int a[] = {4, 2, 5, 1, 3, 6};
    return *min_element(a, a + 6) == 1 ? 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:13: error: 'min_element' was not declared in this scope
    4 |     return *min_element(a, a + 6) == 1 ? 0 : 1;
      |             ^~~~~~~~~~~
g2_unqualified_ptr.cpp:4:13: 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:520:1: note:   'std::min_element'
  520 | min_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
      | ^~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:3102:37: note:   'std::ranges::min_element'
 3102 |   inline constexpr __min_element_fn min_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::min_element itself, shown at its execution-policy declaration in the internal header the transcript names, and the second at std::ranges::min_element. Qualify the name as std::min_element; adding includes does not bring the name out of namespace std.

The same unqualified call with std::array iterators as its arguments fails the same way, for the same underlying reason on this library version - a std::array iterator is a raw pointer here, so it belongs to no namespace either:

#include <algorithm>
#include <array>
#include <iostream>
int main() {
    std::array<int, 6> a = {4, 2, 5, 1, 3, 6};
    std::cout << *min_element(a.begin(), a.end()) << "\n";
    return 0;
}
$ g++ -std=c++20 g_unqualified_array.cpp -o g_unqualified_array_prog
g_unqualified_array.cpp: In function 'int main()':
g_unqualified_array.cpp:6:19: error: 'min_element' was not declared in this scope
    6 |     std::cout << *min_element(a.begin(), a.end()) << "\n";
      |                   ^~~~~~~~~~~
g_unqualified_array.cpp:6:19: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:73,
                 from g_unqualified_array.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:520:1: note:   'std::min_element'
  520 | min_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
      | ^~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:3102:37: note:   'std::ranges::min_element'
 3102 |   inline constexpr __min_element_fn min_element{};
      |                                     ^~~~~~~~~~~

The same unqualified call with std::vector iterators as its arguments behaves differently - it compiles and runs, printing 1, the smallest element:

#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {4, 2, 5, 1, 3, 6};
    std::cout << *min_element(v.begin(), v.end()) << "\n";
    return 0;
}
$ g++ -std=c++20 g_unqualified.cpp -o g_unqualified_prog
$ ./g_unqualified_prog
1

What happened: the vector iterators belong to namespace std, so argument-dependent lookup finds std::min_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, and over this library version's std::array iterators, fails, as the transcripts above show. Write the qualifier either way.

The exact rule

  • std::min_element is declared by #include <algorithm>, and by no other header by rule. In the sweep probed here, every one of the other 100 headers in the library's header set - among them <iostream>, <vector>, <string>, <utility>, <limits>, and <version> - does not declare it, and neither does a file with no include at all.
  • 'min_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 - behind 49 failing sole headers the error line is plain, and behind 51 it carries the suggestion tail did you mean 'tuple_element'?, which names a type trait and is never the fix.
  • In the sweep probed here, the split is three-way: a sole header either fails plainly, fails with the tuple_element suggestion, or rescues the program. No probed sole header produces a declared-but-not-defined link-failure shape for this name - the contrast with std::includes, whose failing set is uniformly plain, and with std::is_sorted, whose suggestion class names is_signed behind 66 headers.
  • In the sweep probed here, only five headers besides <algorithm> rescue std::min_element transitively - <chrono>, <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, those headers are not required to pull std::min_element in.
  • The call returns an iterator, not a value: dereference it for the smallest element. An empty range returns last, a one-element range returns that element, and where the smallest value appears more than once the first occurrence is returned - the fixed programs here print 1 1 1 1 for the hand-checked answer and those three cases, and the search makes exactly max(N-1, 0) comparisons over a range of length N.
  • std::min_element has the plain and comparator overloads, both constexpr since C++17, and execution-policy overloads since C++17: passing std::execution::par with <algorithm> and <execution> included compiles and runs, and a static_assert over the call compiles in a constant expression.
  • std::max_element and std::minmax_element are declared by the same #include <algorithm>: in the programs shown here, which include only <limits> beside the use, each gets its own is not a member of 'std' error when that include is missing, with no header note and no suggestion.
  • std::ranges::min_element is declared by the same #include <algorithm> since C++20: with the include it returns the answer in both its iterator-pair and its single-range call shapes; in the lean context probed here it gets the error 'std::ranges' has not been declared when that include is missing, and behind a sole <vector> it gets 'min_element' is not a member of 'std::ranges'.
  • 'min_element' was not declared in this scope for an unqualified min_element use over raw pointers - and over std::array iterators on this library version - is a different defect: qualify the name as std::min_element - the include alone does not bring the name out of namespace std. The same unqualified use over std::vector iterators 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::min_element. If <algorithm> is not there, add it - that is the fix in the common case, and it holds whether the file failed in a lean context, failed in a textbook file, or happened to compile before through a transitive rescue.
  • Second, do not follow the suggestion when one appears. For this error, in the contexts probed here, the only suggestion GCC prints is tuple_element, behind 51 headers - a class template that names an element type, not a function - so take the fix from the name's home header, <algorithm>, instead, and do not wait for a note naming that header either, because none is printed.
  • Third, do not reach for <tuple> because the suggestion says tuple_element. In the sweep probed here, <tuple> as the only include fails with the same error and the same suggestion - the suggested name's own header does not declare std::min_element either.
  • Fourth, do not reach for <utility> because the error sentence looks like the std::min error. In the sweep probed here, <utility> as the only include fails too, with the suggestion tail - the two names live in different headers, and each takes its own.
  • Fifth, 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 <chrono>, <execution>, <functional>, <regex>, and <valarray> rescue it, and that rescue is not guaranteed.
  • Sixth, dereference the result before comparing or printing it. The call returns an iterator to the smallest element - and on an empty range that iterator is the end iterator, which must not be dereferenced at all.
  • Seventh, an execution policy is allowed here: the policy overloads have existed since C++17, so a policy call that fails is failing for another reason - check that <algorithm> and <execution> are both included before changing the call.
  • Eighth, if the error says was not declared in this scope instead, fix the name's qualifier to std::min_element as well - and do not rely on an unqualified call that happens to compile over std vector iterators, because the same call over raw pointers and over std::array iterators fails.
  • Ninth, if the failing name is a neighboring one, check its header before fixing: max_element and minmax_element take the same added <algorithm>, while min and max take <algorithm> too but answer a different question - the smaller or larger of two values, not the extreme of a range.

Which fix for which cause

  • #include <algorithm> missing, std::min_element used 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 'min_element' is not a member of 'std', with no suggestion, no header note, and no help - add #include <algorithm>.
  • #include <algorithm> missing, std::min_element used in a file whose only include is one of the 51 headers listed as suggestion in the sweep section above: the message is the same error with ; did you mean 'tuple_element'? on the error line - ignore the suggestion and add #include <algorithm>.
  • std::min_element used in a textbook file including <iostream> and <vector>: the message is the same error, with the tuple_element suggestion tail - add #include <algorithm>.
  • std::min_element used with only <algorithm>, <chrono>, <execution>, <functional>, <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::min_element called with an execution policy as its first argument, <algorithm> and <execution> included: no error - the policy overloads exist since C++17 and the call runs.
  • std::max_element or std::minmax_element used without <algorithm> in the lean context: the message names that name, plainly, with no header note - add #include <algorithm>.
  • std::ranges::min_element used without <algorithm> in the lean context: the message is 'std::ranges' has not been declared - add #include <algorithm>. Behind a sole <vector> the message is 'min_element' is not a member of 'std::ranges' - the same fix.
  • min_element used without the std:: qualifier, <algorithm> included, over raw pointers or std::array iterators: the message is 'min_element' was not declared in this scope - qualify the name as std::min_element. Over std::vector iterators 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 search, 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 suggestion, and do not wait for a note naming the header. For this error, in the contexts probed here, the only suggestion printed is the type trait tuple_element, and no note naming <algorithm> appears behind any failing header - take the fix from the name's home header, <algorithm>, instead.
  • Do not import the std::min fix into this error by header: in the probed set, <utility> fails here too. Split neighboring names by home header and by question first - a range search is not a two-value comparison.
  • 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 rescue std::min_element at all, and the five headers that do rescue it do so only transitively.
  • Guard the empty range before dereferencing: the call returns the end iterator there, and the first of any tied smallest elements otherwise.
  • One header per name the file uses: std::vector comes from <vector> and std::string comes from <string>, even in a program whose subject is the search - and std::min_element comes from <algorithm> in return. The comparator overload's std::greater comes from <functional>, and the execution-policy overloads' policy comes from <execution>.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 1 6 for the smallest elements of two ranges, and 1 1 1 1 for the hand-checked answer, the empty range, the one-element range, and the first of two tied minima.

Related guides

  • 'std::min' Is Not a Member of 'std' Fix - the same missing-include shape for the two-value comparison function template in <algorithm>: its own full header sweep, its error, and its fix are covered there, and its name is the one this guide's call is most often confused with.
  • 'std::max' Is Not a Member of 'std' Fix - the same missing-include shape for the larger-of-two-values function template in <algorithm>: its own full header sweep, its error, and its fix are covered there.
  • 'std::includes' Is Not a Member of 'std' Fix - the same missing-include shape for the sorted-range inclusion test in <algorithm>: its own full header sweep, its error, and its fix are covered there, and its sweep is uniformly plain where this guide's sweep splits between a plain class and the tuple_element suggestion class.

Sources

  • cppreference, std::min_element — for std::min_element being a function template defined in <algorithm> that finds the smallest element in a range, with respect to operator< (until C++20) / std::less{} (since C++20) for the plain overload and a comparator overload, both constexpr since C++17, execution-policy overloads since C++17, exactly max(N-1, 0) comparisons for a range of length N, returning an iterator to the smallest element, the first of them if several are equally smallest, and last if the range is empty, with a defect report against C++98 clarifying that empty-range return.
  • cppreference, Standard library header <algorithm> — for <algorithm> being the header that declares std::min_element and the neighboring algorithm names, including the std::ranges::min_element algorithm function object since C++20.
  • cppreference, std::ranges::min_element — for std::ranges::min_element being an algorithm function object defined in <algorithm> since C++20, constexpr, constrained to forward iterators and ranges, in an iterator-sentinel form and a range form, that finds the smallest element in a range and returns an iterator to it, at exactly max(N-1, 0) applications of the comparison.
  • cppreference, std::max_element — for std::max_element being a function template defined in <algorithm> that finds the largest element in a range.
  • cppreference, std::minmax_element — for std::minmax_element being a function template defined in <algorithm> that finds the smallest and the largest elements in a range.