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

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

我运行以下代码

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

std::ranges::sort(randomIntegers);

当我用 g++ -std=c++20 file.cpp 编译时,我得到

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

为什么 sort 不是成员?我正在使用 VScode 智能感知,它显示了 advancebegincommon_view 等方法。但不是排序。

ranges api

但是您可以按如下方式使用排序:

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

要访问 std::ranges::sort,您需要 #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