未在lambda中捕获的变量>

问题描述 投票:-2回答:1

我正在尝试使用STL分区来实现快速选择算法,但是在使用分区函数中的第三个参数时遇到了一些麻烦,有人可以告诉我哪里出了问题以及如何解决吗?点存储在具有v [i] [0]和v [i] [1]值的vector<vector<int>> v中。

代码:

    int partitionR(vector<vector<int>> &v, int lo, int hi) {
        random_device rd;
        default_random_engine gen(rd());
        int pivotIdx = uniform_int_distribution<int>{lo, hi}(gen);
        const int sq = v[pivotIdx][0] * v[pivotIdx][0] + v[pivotIdx][1] * v[pivotIdx][1]; 
        partition(v.begin() + lo, v.begin() + hi, [](const auto &x) {
            return (x[0] * x[0] + x[1] * x[1]) <= sq;});
        return pivotIdx;
    }

    void quickSelect(vector<vector<int>> &v, int K) {
        int lo = 0; int hi = v.size() - 1;
        for(;;) {
            int pivot = partitionR(v, lo, hi);
            if(pivot == K) return;
            else if(pivot > K) hi = pivot - 1;
            else lo = pivot + 1;
        }
    }

在此行中更具体:

        int pivotIdx = uniform_int_distribution<int>{lo, hi}(gen);
        const int sq = v[pivotIdx][0] * v[pivotIdx][0] + v[pivotIdx][1] * v[pivotIdx][1]; 
        partition(v.begin() + lo, v.begin() + hi, [](const auto &x) {
            return (x[0] * x[0] + x[1] * x[1]) <= sq;});

错误是未在lambda中捕获sq。

我正在尝试使用STL分区实现快速选择算法,但是在使用分区函数中的第三个参数时遇到了一些麻烦,有人可以告诉我什么地方错了以及如何...

c++ syntax
1个回答
0
投票

Lambda expressions on CppReference.com。

© www.soinside.com 2019 - 2024. All rights reserved.