C++

'std::sort' Is Not a Member of 'std' Fix

std::sort is a function template declared in the header , and the container and I/O headers around sorting code do not declare it — not , not , and not . Unlike some missing-include errors, GCC prints no note naming the header here, so the fix has to come from knowing the header: add .

Written November 27, 2026 · Tested with GCC 13.3.0 · 9 min read

By the end of this guide
  • The error: the include is missing
  • The same error through neighboring headers: <vector>, <string>, and <utility> are not enough
  • The fix
  • The same fix for strings and for descending order

Written November 27, 2026

Tested with GCC 13.3.0

What this guide is

std::sort is a function template defined in the header <algorithm>. It sorts the elements in the range [first, last) with respect to operator< by default, or with respect to a comparator passed as the third argument, and the order of equivalent elements is not guaranteed to be preserved. The same header also declares std::stable_sort, which does preserve the relative order of equivalent elements, and, since C++20, std::ranges::sort.

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

  • The #include <algorithm> line is missing. GCC does not add a note naming the missing header for this error — its only suggestion is did you mean 'qsort'?, which is a different function and not the fix.

The headers that sit near sorting code — <vector>, <string>, <utility> — do not rescue the program. Each one is tested below, and each fails the same way.

The error: the include is missing

This program sorts a vector of integers but includes only <iostream> and <vector>:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v{3, 1, 2};
    std::sort(v.begin(), v.end());
    for (int x : v) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 noinclude.cpp -o noinclude_prog
noinclude.cpp: In function 'int main()':
noinclude.cpp:5:10: error: 'sort' is not a member of 'std'; did you mean 'qsort'?
    5 |     std::sort(v.begin(), v.end());
      |          ^~~~
      |          qsort

The default compilation mode prints the identical error, so this shape is not a language-mode problem. The qsort suggestion is also a dead end: qsort is the C sorting function, with a different call shape, and the program does not need a different sort — it needs the declaration of the sort it already calls.

The same error through neighboring headers: <vector>, <string>, and <utility> are not enough

Sorting code almost always already includes a container or string header, and those includes feel close enough. They are not. This program adds <string> to the program above, and it fails the same way:

C++
#include <iostream>
#include <string>
#include <vector>
int main() {
    std::vector<int> v{3, 1, 2};
    std::sort(v.begin(), v.end());
    return 0;
}
Terminal
$ g++ -std=c++17 stringonly.cpp -o stringonly_prog
stringonly.cpp: In function 'int main()':
stringonly.cpp:6:10: error: 'sort' is not a member of 'std'; did you mean 'qsort'?
    6 |     std::sort(v.begin(), v.end());
      |          ^~~~
      |          qsort

The same result holds for the other neighboring headers tested, each with the same error:

  • With only <iostream>, <utility>, and <vector> included, the program fails with 'sort' is not a member of 'std'.
  • With only <iostream>, <numeric>, and <vector> included, the program fails with 'sort' is not a member of 'std'.
  • With only <iostream>, <array>, and <vector> included, the program fails with 'sort' is not a member of 'std'.
  • With only <iostream>, <iterator>, and <vector> included, the program fails with 'sort' is not a member of 'std'.
  • With only <iostream>, <memory>, and <vector> included, the program fails with 'sort' is not a member of 'std'.
  • With only <iostream>, <ranges>, and <vector> included, in C++20 mode, the program fails with 'sort' is not a member of 'std' — <ranges> does not declare std::sort either.

One neighboring header is the exception on this toolchain, and it is an accident, not a rule: with <functional> included, the program above compiles and runs on GCC 13.3.0, because that library version happens to pull the declaration of std::sort in as a side effect. No standard header is required to declare std::sort on behalf of <algorithm>, and a program that relies on the <functional> side effect can break under a different library version. Include <algorithm> in every file that calls std::sort, std::stable_sort, or std::ranges::sort.

The fix

Add the header:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v{3, 1, 2};
    std::sort(v.begin(), v.end());
    for (int x : v) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 good.cpp -o good_prog
$ ./good_prog
1 2 3

The same program also compiles and runs with no -std flag at all. The two arguments are the pair of iterators defining the range to sort — v.begin() and v.end() for the whole vector — and they stay the same whatever element type the container holds.

The same fix for strings and for descending order

The header fixes more than the integer case, because sorting strings uses the same function template and the same declaration. This program sorts a vector of strings, and the elements order themselves by their own operator<:

C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
    std::vector<std::string> v{"pear", "apple", "fig"};
    std::sort(v.begin(), v.end());
    for (const auto& s : v) std::cout << s << " ";
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 strings.cpp -o strings_prog
$ ./strings_prog
apple fig pear

To sort in the other direction, pass a comparator as the third argument. std::greater is a function object type defined in <functional> that invokes operator> on its arguments, and a lambda returning a > b does the same job inline. This program sorts the same vector both ways, and both passes print the same descending order:

C++
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v{3, 1, 2};
    std::sort(v.begin(), v.end(), std::greater<int>());
    for (int x : v) std::cout << x << " ";
    std::cout << "\n";
    std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });
    for (int x : v) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 descending.cpp -o descending_prog
$ ./descending_prog
3 2 1
3 2 1

The comparator form is the second overload of std::sort: elements are sorted with respect to the comparator instead of operator<. Include <functional> in any file that names std::greater, because <functional> is the header that defines it.

The neighboring errors: std::stable_sort and std::ranges::sort

The same missing include is worded for whichever sort name the program calls. With std::stable_sort — the variant that guarantees the relative order of equivalent elements is preserved, also defined in <algorithm> — the error names that function instead, and GCC offers no suggestion at all:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v{3, 1, 2};
    std::stable_sort(v.begin(), v.end());
    return 0;
}
Terminal
$ g++ -std=c++17 stable_noinclude.cpp -o stable_noinclude_prog
stable_noinclude.cpp: In function 'int main()':
stable_noinclude.cpp:5:10: error: 'stable_sort' is not a member of 'std'
    5 |     std::stable_sort(v.begin(), v.end());
      |          ^~~~~~~~~~~

In C++20 mode, std::ranges::sort — also defined in <algorithm> since C++20 — fails with its own wording when the include is missing, and including <ranges> alone does not fix it either, because the ranges algorithms are declared by <algorithm>, not by <ranges>:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v{3, 1, 2};
    std::ranges::sort(v);
    return 0;
}
Terminal
$ g++ -std=c++20 ranges_noinclude.cpp -o ranges_noinclude_prog
ranges_noinclude.cpp: In function 'int main()':
ranges_noinclude.cpp:5:18: error: 'sort' is not a member of 'std::ranges'
    5 |     std::ranges::sort(v);
      |                  ^~~~

In C++17 mode the same ranges program fails earlier in the name, with 'std::ranges' has not been declared, because the std::ranges namespace itself is a C++20 feature. Both wordings have the same fix in C++20 mode: add #include <algorithm>, after which the ranges program compiles, runs, and prints 1 2 3. The std::stable_sort program likewise compiles, runs, and prints 1 2 3 once <algorithm> is added.

A different wording: sort without the std::

Calling the function without its namespace qualifier is a different defect, and GCC words it differently:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v{3, 1, 2};
    sort(v.begin(), v.end());
    return 0;
}
Terminal
$ g++ -std=c++17 declared_variant.cpp -o declared_variant_prog
declared_variant.cpp: In function 'int main()':
declared_variant.cpp:5:5: error: 'sort' was not declared in this scope; did you mean 'short'?
    5 |     sort(v.begin(), v.end());
      |     ^~~~
      |     short

'sort' was not declared in this scope is the 'has not been declared' family for this topic: an unqualified name that lookup cannot find at all. Adding <algorithm> alone does not fix this program, because the declaration it adds is std::sort, inside namespace std — the call must also be qualified as std::sort, or the file must bring the name in deliberately. If your error says is not a member of 'std', the name was qualified and the include is the whole problem; if it says was not declared in this scope, check the qualifier first.

The exact rule

  • std::sort, std::stable_sort, and std::ranges::sort are declared by #include <algorithm>, and nowhere else by rule. <iostream>, <vector>, <string>, <utility>, <numeric>, <array>, <iterator>, <memory>, and <ranges> do not declare std::sort.
  • 'sort' is not a member of 'std' with the suggestion did you mean 'qsort'? means the <algorithm> include is missing. GCC prints no header note for this error — do not wait for one, and do not switch to qsort.
  • On GCC 13.3.0, <functional> happens to pull std::sort in transitively, so a program can compile with only <functional> included. That is a side effect of this library version, not a declaration to rely on.
  • 'sort' was not declared in this scope for an unqualified sort(...) call is a different defect: qualify the call as std::sort and include <algorithm>.

The fixes, ranked

  • First, check the include list of the file that calls std::sort. If <algorithm> is not there, add it — that is the fix in the common case.
  • Second, ignore the qsort suggestion. It points at a different function, not at the missing header.
  • Third, if the error says was not declared in this scope instead, fix the call's qualifier to std::sort as well as the include.
  • Fourth, if the file compiles only because <functional> is included, add <algorithm> anyway, so the program does not depend on a transitive include.

Which fix for which cause

  • #include <algorithm> missing, std::sort called with <vector> present: the message is 'sort' is not a member of 'std' — add #include <algorithm>.
  • Only <string>, <utility>, <numeric>, <array>, <iterator>, <memory>, or <ranges> included: same message — add #include <algorithm>.
  • std::stable_sort called without <algorithm>: the message is 'stable_sort' is not a member of 'std' — add #include <algorithm>.
  • std::ranges::sort called without <algorithm> in C++20 mode: the message is 'sort' is not a member of 'std::ranges' — add #include <algorithm>.
  • sort(...) called without the std:: qualifier: the message is 'sort' was not declared in this scope — qualify the call as std::sort and include <algorithm>.

Checklist

  • Check the include list of the file that calls the 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 did you mean 'qsort'? suggestion. The program needs the declaration of std::sort, not a different sorting function.
  • Do not rely on <functional> pulling std::sort in. It does on this toolchain, as a side effect; <algorithm> is the header that declares it.
  • If the program also sorts in descending order with std::greater, include <functional> for that name as well — one header per name the file uses.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 1 2 3, apple fig pear, and 3 2 1.
  • If equivalent elements must keep their original relative order, switch to std::stable_sort — same header, same fix, different guarantee.

Sources

  • cppreference, std::sort — for std::sort being defined in <algorithm>, for sorting the range [first, last) with respect to operator< by default or a passed comparator, and for the order of equivalent elements not being guaranteed.
  • cppreference, Standard library header <algorithm> — for <algorithm> declaring the sorting algorithms, including std::sort, std::stable_sort, and the ranges algorithms.
  • cppreference, std::stable_sort — for std::stable_sort being defined in <algorithm> and guaranteeing that the order of equivalent elements is preserved.
  • cppreference, std::ranges::sort — for std::ranges::sort being defined in <algorithm> since C++20 and sorting a range with respect to a comparator and projection.
  • cppreference, std::greater — for std::greater being defined in <functional> and invoking operator> on its arguments.

Keep Exploring