Search code examples
c++c++20std-rangesg++10

error: 'sort' is not a member of 'std::ranges'; did you mean 'std::sort'?


I ran the following code

vector<int> randomIntegers = generateIntegers(10); // Generates 10 integers

std::ranges::sort(randomIntegers);

When I compile with g++ -std=c++20 file.cpp , I get

error: 'sort' is not a member of 'std::ranges'; did you mean 'std::sort'?
  • gcc --version: gcc 10.2.0
  • g++ --version: g++ 10.2.0

Why is sort not a member? I'm using VScode intellisense, and it shows methods such as advance,begin,common_view. But not sort.


Solution

  • To get access to std::ranges::sort you need to #include <algorithm>:

    #include <algorithm>
    #include <vector>
    
    int main() {
        std::vector<int> randomIntegers{9,8,7,6,5,4,3,2,1,0}; // some integers
    
        std::ranges::sort(randomIntegers);
    }
    

    Demo