C++

'partial_sort' Is Not a Member of 'std' Fix

std::partial_sort lives in one header, <algorithm> - and on this library version almost no ordinary file rescues it by accident: in the sweep probed here, covering all 106 standard headers in the library's header set, only <functional>, <chrono>, <execution>, <regex>, and <valarray> pull the name in on the side, and the textbook combination of <iostream> and <vector> does not. The error therefore surfaces in textbook files as well as lean ones, and in the sweep probed here it splits three ways - the plain error, the same error with a 'did you mean 'partial_sum'?' tail behind exactly two headers, or one of those five transitive rescues - with no note lines, no help lines, and no link-failure shape behind any probed header. This guide pins down all three outcomes, which probed headers cause each one, and the one include that fixes the error by specification.

Written December 1, 2026 · Tested with GCC 13.3.0 · 23 min read

By the end of this guide
  • The error: the include is missing, in a lean file
  • A neighboring header does not rescue it: only &lt;numeric&gt;
  • The textbook version fails too - plainly
  • Neighboring headers: which ones fail plainly, which ones add the suggestion, which ones rescue it

Written December 1, 2026

Tested with GCC 13.3.0

What this guide is

std::partial_sort is a function template defined in the header <algorithm>. It places the first middle - first elements of the range [first, last) into [first, middle) in sorted order, and leaves the remaining elements in [middle, last) in an unspecified order. It returns nothing. The plain overloads are constexpr since C++20; a second family of overloads, since C++17, takes an execution policy as its first argument, and a comparator overload orders by a caller-supplied comparison instead of operator<. Its iterators must be random-access iterators, and it makes approximately N·log(M) comparisons, where N is the length of the whole range and M is the length of the sorted prefix. To place only the single element that belongs at one position instead, std::nth_element is the tool.

Because the template lives in one specific header, 'partial_sort' is not a member of 'std' has one common cause:

  • The #include <algorithm> line is missing, and no other header in the file pulls the name in transitively. For this error, in the contexts probed here, GCC never prints a note naming <algorithm>, and it never prints note lines or help lines under the error - but in two sole-header contexts it does print a suggestion tail on the error line, and the suggestion is a trap: did you mean 'partial_sum'? points at std::partial_sum, a function template in <numeric> that computes running sums into an output range, not at the algorithm the program means. The neighboring names probed here - std::nth_element, std::partition, std::sort, and std::rotate, all in the same header - get no suggestion at all in the same lean context, as the neighboring-names section shows.

What makes this error worth separating from its relatives is the shape of the sweep behind it. The sweep probed here covers every one of the 106 standard headers in this library's header set, compiled as the only header in the program, and the outcome splits three ways - the plain compile error, the same error with the partial_sum suggestion tail, or a transitive rescue. The suggestion class here is tiny - 2 of the 100 failing sole headers, <numeric> and <random> - where its closest sibling in this family, std::nth_element, carries a suggestion tail behind 51 failing headers. The rescue side matches the family pattern exactly: std::nth_element, std::rotate, and std::partition, probed in their own guides, are rescued by <functional>, <chrono>, <execution>, <regex>, and <valarray>, and std::partial_sort is rescued by the same five, no more, across the full header set. The older siblings in this family - std::nth_element, std::rotate, std::partition, and std::sort - 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 partially sorts an array's first three elements with std::partial_sort and returns success if those elements land as 1, 2, 3, but includes only <limits>:

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

That is the whole output: one error, no suggestion on the error line, no note lines, no help lines. The same program with no include at all fails the same way:

C++
int main() {
    int a[] = {5, 1, 4, 2, 3, 6};
    std::partial_sort(a, a + 3, a + 6);
    return a[0] == 1 && a[1] == 2 && a[2] == 3 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:10: error: 'partial_sort' is not a member of 'std'
    3 |     std::partial_sort(a, a + 3, a + 6);
      |          ^~~~~~~~~~~~

A neighboring header does not rescue it: only <numeric>

A reader who knows the <numeric> algorithms might reach for <numeric> first - it declares the similarly named std::partial_sum. In the program probed here it does not rescue the call - and it changes the error's shape, adding the partial_sum suggestion tail:

C++
#include <numeric>
int main() {
    int a[] = {5, 1, 4, 2, 3, 6};
    std::partial_sort(a, a + 3, a + 6);
    return a[0] == 1 && a[1] == 2 && a[2] == 3 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 m_numeric.cpp -o m_numeric_prog
m_numeric.cpp: In function 'int main()':
m_numeric.cpp:4:10: error: 'partial_sort' is not a member of 'std'; did you mean 'partial_sum'?
    4 |     std::partial_sort(a, a + 3, a + 6);
      |          ^~~~~~~~~~~~
      |          partial_sum

That is expected once the homes are separated: <numeric> declares std::partial_sum, and std::partial_sort is not one of its names. The suggestion appears here because <numeric> makes std::partial_sum visible, and partial_sum is close enough to partial_sort in spelling for GCC to offer it - it is still the wrong name, a running-sum algorithm that writes to an output iterator, not a sort. The two names share a prefix and a sentence pattern, but each takes its own header, and the fix below is <algorithm>, not the numeric fix.

The textbook version fails too - plainly

The version most often written first - partially sort a vector's first three elements and print them, with std::cout - gets the same error, without <algorithm>:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {5, 1, 4, 2, 3, 6};
    std::partial_sort(v.begin(), v.begin() + 3, v.end());
    std::cout << v[0] << " " << v[1] << " " << v[2] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:10: error: 'partial_sort' is not a member of 'std'
    5 |     std::partial_sort(v.begin(), v.begin() + 3, v.end());
      |          ^~~~~~~~~~~~

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 here the error is plain, with no suggestion tail at all, because the textbook headers do not make std::partial_sum visible. In the sweep probed here, the only headers that rescue std::partial_sort are <functional>, <chrono>, <execution>, <regex>, and <valarray>, besides <algorithm> itself, and that rescue is a property of this library version, not a rule of the language. A program that uses std::partial_sort includes <algorithm>, whether or not it happens to compile without it.

Neighboring headers: which ones fail plainly, which ones add the suggestion, which ones rescue it

In a sweep where each of the 106 standard headers in this library's header set was compiled as the only header in a program that partially sorts an array's first three elements and returns whether those elements hold 1, 2, 3, the outcome splits by compile result and diagnostic shape three ways. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 98 headers fail with the plain is not a member of 'std' error, with no suggestion, no note, and no help:

  • <any>
  • <array>
  • <atomic>
  • <barrier>
  • <bit>
  • <bitset>
  • <cassert>
  • <ccomplex>
  • <cctype>
  • <cerrno>
  • <cfenv>
  • <cfloat>
  • <charconv>
  • <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>
  • <optional>
  • <ostream>
  • <queue>
  • <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. And these 2 headers fail with the same error plus the suggestion tail ; did you mean 'partial_sum'? on the error line - still with no note lines and no help lines:

  • <numeric>
  • <random>

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

C++
#include <vector>
int main() {
    int a[] = {5, 1, 4, 2, 3, 6};
    std::partial_sort(a, a + 3, a + 6);
    return a[0] == 1 && a[1] == 2 && a[2] == 3 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 v_vector.cpp -o v_vector_prog
v_vector.cpp: In function 'int main()':
v_vector.cpp:4:10: error: 'partial_sort' is not a member of 'std'
    4 |     std::partial_sort(a, a + 3, a + 6);
      |          ^~~~~~~~~~~~

And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:

  • <algorithm>
  • <chrono>
  • <execution>
  • <functional>
  • <regex>
  • <valarray>

Two contrasts with the neighboring sweeps are worth holding on to. First, there is no link-failure shape in this sweep: every failing sole header fails at compile time with the error above - each one was also compiled to an object file on its own, and that step failed the same way - so the declared-but-not-defined link failure that std::swap produces behind five headers has no counterpart for this name in the probed set. Second, the suggestion class is this name's own trap, and it is narrow: the 2 headers in that class are the ones that make std::partial_sum visible - <numeric>, its home header, and <random> - and GCC offers the closest visible spelling. <numeric> itself is in the suggestion class, not the rescue class - including the header the suggestion's name lives in still does not declare std::partial_sort. The closest relative, std::nth_element, was swept in full in its own guide: it fails plainly with <limits> and is rescued by <algorithm>, <functional>, <chrono>, <execution>, <regex>, and <valarray> - the same rescue set as here, though its suggestion class is far larger. The rescue list matches the short lists in this family: <algorithm> - the home header - is on this list by definition, not by transitivity, and every other rescue on that list is this library version's behavior, not a guarantee.

The fix

Add the header:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {5, 1, 4, 2, 3, 6};
    std::partial_sort(v.begin(), v.begin() + 3, v.end());
    std::cout << v[0] << " " << v[1] << " " << v[2];
    for (int x : v) std::cout << " " << x;
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
1 2 3 1 2 3 5 4 6

One include, <algorithm>, declares the template. Partially sorting 5, 1, 4, 2, 3, 6 with the split after the third element puts the three smallest values, 1, 2, 3, in the first three positions in sorted order, and leaves the remaining values 5, 4, 6 after them in no particular order, so the program prints 1 2 3 followed by the full contents 1 2 3 5 4 6.

The fixed programs: the sorted prefix follows the split

With <algorithm> added, std::partial_sort sorts as many leading elements as the split position asks for. Partially sorting three copies of the same vector with splits after positions 1, 3, and 6 sorts one element, three elements, and - at a split equal to the end - the whole vector:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> a = {5, 1, 4, 2, 3, 6};
    std::vector<int> b = {5, 1, 4, 2, 3, 6};
    std::vector<int> c = {5, 1, 4, 2, 3, 6};
    std::partial_sort(a.begin(), a.begin() + 1, a.end());
    std::partial_sort(b.begin(), b.begin() + 3, b.end());
    std::partial_sort(c.begin(), c.begin() + 6, c.end());
    std::cout << a[0] << " | " << b[0] << " " << b[1] << " " << b[2] << " | " << c[0] << " " << c[5] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 i_cases.cpp -o i_cases_prog
$ ./i_cases_prog
1 | 1 2 3 | 1 6

The two boundary splits behave as the rule says. When middle equals first, no element is required to be sorted and the call rearranges nothing the program can observe - the vector still holds a permutation of its elements; when middle equals last, the call is a full sort - 1 first and 6 last here:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> a = {5, 1, 4, 2, 3, 6};
    std::vector<int> b = {5, 1, 4, 2, 3, 6};
    std::partial_sort(a.begin(), a.begin(), a.end());
    std::partial_sort(b.begin(), b.end(), b.end());
    bool perm = true;
    for (int x : a) perm = perm && (x >= 1 && x <= 6);
    std::cout << perm << " " << b[0] << " " << b[5] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_edges.cpp -o k_edges_prog
$ ./k_edges_prog
1 1 6

The sorted-prefix property is exact, not just correct values in the prefix: after partially sorting by a split after position 3, the first three elements are 1, 2, 3 in order, and every one of them is at most every element after the split, which this program checks element by element and reports as 1, alongside the prefix itself:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {5, 1, 4, 2, 3, 6};
    std::partial_sort(v.begin(), v.begin() + 3, v.end());
    bool ok = (v[0] == 1 && v[1] == 2 && v[2] == 3);
    for (int i = 0; i < 3; ++i)
        for (int j = 3; j < 6; ++j) ok = ok && (v[i] <= v[j]);
    std::cout << ok << " " << v[0] << " " << v[1] << " " << v[2] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_values.cpp -o k_values_prog
$ ./k_values_prog
1 1 2 3

The execution-policy overload - the second family of overloads, since C++17 - takes the policy as its first argument and needs <execution> for the policy itself. With std::execution::par the same partial sort puts the same prefix, 1, 2, 3, in the first three positions:

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

The comparator overload orders by the comparison the program passes instead: with std::greater<int>() from <functional>, the first three positions hold the three largest values in descending order, 6, 5, 4:

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

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

The names that std::partial_sort is built alongside share its home header. In the lean context here - only <limits> included - they get no suggestion at all. std::nth_element - the function template defined in <algorithm> that places the element that belongs at one position there, without sorting either side - gets its own error, again with no header note:

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

std::partition - the function template defined in <algorithm> that reorders a range so the elements satisfying a predicate come first - fails the same way:

C++
#include <limits>
int main() {
    int a[] = {5, 1, 4, 2, 3, 6};
    return std::partition(a, a + 6, [](int x) { return x % 2 == 0; }) - a == 3 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 c_partition.cpp -o c_partition_prog
c_partition.cpp: In function 'int main()':
c_partition.cpp:4:17: error: 'partition' is not a member of 'std'
    4 |     return std::partition(a, a + 6, [](int x) { return x % 2 == 0; }) - a == 3 ? 0 : 1;
      |                 ^~~~~~~~~

std::sort - the function template defined in <algorithm> that sorts a range into ascending order - fails the same way:

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

And std::rotate - the function template defined in <algorithm> that rotates the elements of a range left at the middle position - fails the same way, again with no header note:

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

One contrast is worth holding on to: in the lean programs here, partial_sort, nth_element, partition, sort, and rotate all get no note and - in this <limits> context - no suggestion. The partial_sum suggestion appears only in contexts where a header in the file makes that name visible, as the sweep section shows; do not read its presence as a fix, or its absence as a puzzle - in this probed set the fix comes from the names' home, <algorithm>, either way.

The neighboring names, fixed

And the neighboring names work under the same one added header: partitioning 5, 1, 4, 2, 3, 6 around its third element puts 3 there, sorting the vector and rotating it left by two puts 3 first, and partitioning that rotated range by even puts the partition point at index 3:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {5, 1, 4, 2, 3, 6};
    std::nth_element(v.begin(), v.begin() + 2, v.end());
    int nth = v[2];
    std::sort(v.begin(), v.end());
    std::rotate(v.begin(), v.begin() + 2, v.end());
    std::cout << nth << " " << v[0] << " " << (std::partition(v.begin(), v.end(), [](int x) { return x % 2 == 0; }) - v.begin()) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_neighbors_fixed.cpp -o k_neighbors_fixed_prog
$ ./k_neighbors_fixed_prog
3 3 3

Since C++20 the same <algorithm> header also declares std::ranges::partial_sort, the ranges form of the same partial sort. It comes in two call shapes - an iterator triple, and a range plus the middle iterator - and returns the past-the-end iterator of the range: here both shapes put 1, 2, 3 in the first three positions. 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:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {5, 1, 4, 2, 3, 6};
    std::vector<int> w = {5, 1, 4, 2, 3, 6};
    std::ranges::partial_sort(v.begin(), v.begin() + 3, v.end());
    std::ranges::partial_sort(w, w.begin() + 3);
    std::cout << v[0] << " " << v[2] << " " << w[0] << " " << w[2] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
1 3 1 3
C++
#include <limits>
int main() {
    int a[] = {5, 1, 4, 2, 3, 6};
    std::ranges::partial_sort(a, a + 3, a + 6);
    return a[0] == 1 && a[2] == 3 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 r2_ranges_lean.cpp -o r2_ranges_lean_prog
r2_ranges_lean.cpp: In function 'int main()':
r2_ranges_lean.cpp:4:10: error: 'std::ranges' has not been declared
    4 |     std::ranges::partial_sort(a, a + 3, a + 6);
      |          ^~~~~~

A different wording - and a case where the qualifier is not needed

Including <algorithm> is not the only half of the name - but for this function the unqualified form behaves two ways, and which one a program gets depends on its arguments. This program includes the header, drops the std:: qualifier, and passes raw pointers into a C array:

C++
#include <algorithm>
int main() {
    int a[] = {5, 1, 4, 2, 3, 6};
    partial_sort(a, a + 3, a + 6);
    return a[0] == 1 && a[2] == 3 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 g2_unqualified_ptr.cpp -o g2_unqualified_ptr_prog
g2_unqualified_ptr.cpp: In function 'int main()':
g2_unqualified_ptr.cpp:4:5: error: 'partial_sort' was not declared in this scope
    4 |     partial_sort(a, a + 3, a + 6);
      |     ^~~~~~~~~~~~
g2_unqualified_ptr.cpp:4:5: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:73,
                 from g2_unqualified_ptr.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:364:1: note:   'std::partial_sort'
  364 | partial_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __middle,
      | ^~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:1872:38: note:   'std::ranges::partial_sort'
 1872 |   inline constexpr __partial_sort_fn partial_sort{};
      |                                      ^~~~~~~~~~~~

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::partial_sort itself, shown at its execution-policy declaration in the internal header the transcript names, and the second at std::ranges::partial_sort. Qualify the name as std::partial_sort; adding includes does not bring the name out of namespace std.

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

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

What happened: the iterators belong to namespace std, so argument-dependent lookup finds std::partial_sort from the arguments themselves, even without the qualifier. That is a property of the argument types, not a style to rely on: the same call over raw pointers fails, as the transcript above shows. Write the qualifier either way.

The exact rule

  • std::partial_sort is declared by #include <algorithm>, and by no other header by rule. In the sweep probed here, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , do not declare it, and neither does a file with no include at all.
  • 'partial_sort' is not a member of 'std' means the <algorithm> include is missing and nothing else in the file pulled the name in. In the contexts probed here, GCC prints no note naming <algorithm>, no note lines, and no help lines under it; the only diagnostic tail in the probed set is the suggestion did you mean 'partial_sum'?, which appears in the 2 sole-header contexts listed in the sweep section, and names a function template in <numeric>, not the partial sort.
  • In the sweep probed here, the split is three-way: a sole header either fails plainly, fails with the partial_sum suggestion tail, or rescues the program. Each failing sole header also failed when compiled to an object file on its own, so the probed set contains no declared-but-not-defined link-failure shape for this name.
  • In the sweep probed here, only five headers besides <algorithm> rescue std::partial_sort transitively - <functional>, <chrono>, <execution>, <regex>, and <valarray> - so a file whose only include is one of those compiles and runs the call without <algorithm>. That is this library version's behavior, not a guarantee - in the probed set, those headers are not required to pull std::partial_sort in.
  • std::nth_element, std::partition, std::sort, and std::rotate 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::partial_sort is declared by the same #include <algorithm> since C++20: with the include it partially sorts the range and returns the past-the-end iterator, in both its iterator-triple and its range-plus-middle call shapes, and in the lean context probed here it gets the error 'std::ranges' has not been declared when that include is missing.
  • 'partial_sort' was not declared in this scope for an unqualified partial_sort use over raw pointers is a different defect: qualify the name as std::partial_sort - 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::partial_sort. If <algorithm> is not there, add it - that is the fix in the common case, and it holds whether the file failed plainly, failed with the suggestion tail, or happened to compile before through a transitive rescue.
  • Second, do not follow the partial_sum suggestion. In the contexts probed here where it appears, it names a running-sum function template in <numeric> - it takes an output iterator and writes sums, it does not sort anything, and adding <numeric> does not declare std::partial_sort either: in the sweep here <numeric> itself is in the suggestion class, not the rescue class.
  • Third, do not reach for <utility> because the error sentence looks like the std::swap error. In the sweep probed here, <utility> as the only include fails plainly with the same error - the two names live in different headers, and each takes its own.
  • Fourth, do not be reassured by <iostream>, <vector>, or <string> being present - in the sweep here none of them rescues this name at all - and do not remove <algorithm> because a file compiles without it: in the probed set only <functional>, <chrono>, <execution>, <regex>, and <valarray> rescue it, and that rescue is not guaranteed.
  • Fifth, if the error says was not declared in this scope instead, fix the name's qualifier to std::partial_sort as well - and do not rely on an unqualified call that happens to compile over std iterators, because the same call over raw pointers fails.
  • Sixth, if the failing name is a neighboring one, check its header before fixing: nth_element, partition, sort, and rotate take the same added <algorithm>, while swap takes <utility>.

Which fix for which cause

  • #include <algorithm> missing, std::partial_sort used in a file whose only include is one of the 98 headers listed as plain in the sweep section above, or that includes nothing: the message is 'partial_sort' is not a member of 'std', with no suggestion, no header note, and no help - add #include <algorithm>.
  • #include <algorithm> missing, std::partial_sort used in a file whose only include is <numeric> or <random>: the message is the same error with the tail ; did you mean 'partial_sum'? - ignore the suggestion and add #include <algorithm>.
  • std::partial_sort used in a textbook file including <iostream> and <vector>: the message is the same error, plainly, with no suggestion tail - add #include <algorithm>.
  • std::partial_sort used with only <algorithm>, <functional>, <chrono>, <execution>, <regex>, or <valarray> included: no error on this library version - add #include <algorithm> anyway when it is not the include already present, because the rescue is not guaranteed.
  • std::nth_element, std::partition, std::sort, or std::rotate used without <algorithm> in the lean context: the message names that name, plainly, with no header note - add #include <algorithm>.
  • std::ranges::partial_sort used without <algorithm> in the lean context: the message is 'std::ranges' has not been declared - add #include <algorithm>.
  • partial_sort used without the std:: qualifier, <algorithm> included, over raw pointers: the message is 'partial_sort' was not declared in this scope - qualify the name as std::partial_sort. 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 partial sort, 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 partial_sum suggestion, and do not wait for a note naming the header. For this error, in the contexts probed here, GCC prints no note naming <algorithm> - take the fix from the name's home header, <algorithm>, instead.
  • Do not import the std::swap fix into this error: in the probed set, <utility> fails here too, plainly. Split neighboring names by home header first.
  • Do not assume the textbook includes rescue this name, and do not delete a working <algorithm> line because a file compiles without it. In the sweep here <iostream> and <vector> do not rescue std::partial_sort at all, and the five headers that do rescue it do so only transitively.
  • 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 partial sort - and std::partial_sort comes from <algorithm> in return. The execution-policy overload additionally takes <execution> for the policy object itself, and the comparator overload's std::greater comes from <functional>.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 1 2 3 1 2 3 5 4 6 for the sorted prefix and the full contents, and 1 1 2 3 for the sorted-prefix property check and the prefix it found.
  • 'std::nth_element' Is Not a Member of 'std' Fix - the same missing-include shape for the single-position partitioning function template in <algorithm>: its full header sweep splits three ways with a suggestion tail behind 51 failing headers, where this guide's sweep carries the tail behind only 2.
  • 'std::sort' Is Not a Member of 'std' Fix - the same missing-include shape for the full sorting function template in <algorithm>, an older sibling in this family whose own guide covers its error and its fix.
  • 'std::rotate' Is Not a Member of 'std' Fix - the same missing-include shape for the left-rotation function template in <algorithm>, an older sibling in this family whose own guide covers its error and its fix.
  • 'std::partition' Is Not a Member of 'std' Fix - the same missing-include shape for the predicate reordering in <algorithm>: its sweep produced exactly the same five transitive rescues as this guide's full header sweep.

Sources

  • cppreference, std::partial_sort — for std::partial_sort being a function template defined in <algorithm> that places the first middle - first elements of [first, last) into [first, middle) in sorted order and leaves the rest in an unspecified order, returning nothing, with the plain overloads constexpr since C++20, execution-policy overloads since C++17, a comparator overload, random-access iterators required, and approximately N·log(M) comparisons.
  • cppreference, Standard library header <algorithm> — for <algorithm> being the header that declares std::partial_sort and the neighboring algorithm names, including the std::ranges::partial_sort algorithm function object since C++20.
  • cppreference, std::ranges::partial_sort — for std::ranges::partial_sort being an algorithm function object defined in <algorithm> since C++20, in an iterator-triple form and a range-plus-middle form, that performs the same partial sort and returns the past-the-end iterator of the range.
  • cppreference, std::partial_sum — for std::partial_sum being a function template defined in <numeric> that computes the partial sums of a range and stores them in an output range - the name GCC's suggestion tail points at, not a sort.
  • cppreference, std::nth_element — for std::nth_element being a function template defined in <algorithm> that rearranges [first, last) so the element at nth is the element that would be there if the range were sorted.
  • cppreference, std::sort — for std::sort being a function template defined in <algorithm> that sorts the elements in a range into ascending order.
  • cppreference, std::rotate — for std::rotate being a function template defined in <algorithm> that rotates the elements of a range left at the middle position.
  • cppreference, std::partition — for std::partition being a function template defined in <algorithm> that reorders the elements in [first, last) so all elements for which the unary predicate returns true come before all elements for which it returns false, returning the iterator to the partition point.

Keep Exploring