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

2k views Asked by At

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.

2

There are 2 answers

0
Ted Lyngmo On BEST ANSWER

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

0
Jose Manuel de Frutos On

ranges api

However you can use sort as follows:

#include <algorithm>
std::sort(randomIntegers.begin(), randomIntegers.end());