C++
'std::sort' Is Not a Member of 'std' Fix
std::sort is a function template declared in the header
Written November 27, 2026 · Tested with GCC 13.3.0 · 9 min read
- 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 isdid 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>:
#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;
}
$ 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:
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<int> v{3, 1, 2};
std::sort(v.begin(), v.end());
return 0;
}
$ 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 declarestd::sorteither.
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:
#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;
}
$ 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<:
#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;
}
$ 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:
#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;
}
$ 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:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{3, 1, 2};
std::stable_sort(v.begin(), v.end());
return 0;
}
$ 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>:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{3, 1, 2};
std::ranges::sort(v);
return 0;
}
$ 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:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{3, 1, 2};
sort(v.begin(), v.end());
return 0;
}
$ 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, andstd::ranges::sortare declared by#include <algorithm>, and nowhere else by rule.<iostream>,<vector>,<string>,<utility>,<numeric>,<array>,<iterator>,<memory>, and<ranges>do not declarestd::sort.'sort' is not a member of 'std'with the suggestiondid 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 toqsort.- On GCC 13.3.0,
<functional>happens to pullstd::sortin 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 scopefor an unqualifiedsort(...)call is a different defect: qualify the call asstd::sortand 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
qsortsuggestion. It points at a different function, not at the missing header. - Third, if the error says
was not declared in this scopeinstead, fix the call's qualifier tostd::sortas 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::sortcalled 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_sortcalled without<algorithm>: the message is'stable_sort' is not a member of 'std'— add#include <algorithm>.std::ranges::sortcalled without<algorithm>in C++20 mode: the message is'sort' is not a member of 'std::ranges'— add#include <algorithm>.sort(...)called without thestd::qualifier: the message is'sort' was not declared in this scope— qualify the call asstd::sortand 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 ofstd::sort, not a different sorting function. - Do not rely on
<functional>pullingstd::sortin. 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, and3 2 1. - If equivalent elements must keep their original relative order, switch to
std::stable_sort— same header, same fix, different guarantee.
Related guides
- C++ STL Containers: Vector, Map, and Set — the containers whose iterators
std::sorttakes as its range arguments. - C++23 std::expected Error-Handling Pipelines — another standard-library feature whose availability turns on the header and the language mode, not on the compiler's age alone.
- C++ Expected Unqualified-Id Fix — a neighboring front-end error where the compiler's wording points at the wrong half of the defect.
Sources
- cppreference,
std::sort— forstd::sortbeing defined in<algorithm>, for sorting the range[first, last)with respect tooperator<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, includingstd::sort,std::stable_sort, and the ranges algorithms. - cppreference,
std::stable_sort— forstd::stable_sortbeing defined in<algorithm>and guaranteeing that the order of equivalent elements is preserved. - cppreference,
std::ranges::sort— forstd::ranges::sortbeing defined in<algorithm>since C++20 and sorting a range with respect to a comparator and projection. - cppreference,
std::greater— forstd::greaterbeing defined in<functional>and invokingoperator>on its arguments.
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.