首页 文章

如何将STL算法的lambdas绑定到C风格的多维数组?

提问于
浏览
0

我一直在尝试使用 STL 算法来处理多维数组的元素,似乎没有任何东西可以绑定它们 . 我该怎么做呢:

// Declaration of pix:
float pix[1000][2];

// (...)

const int sizeToParse = 300;
static auto colLessThan = [] 
    (const float coordPair_lhs[2], const float coordPair_rhs[2]) -> bool 
    // (const float** coordPair_lhs, const float** coordPair_rhs) -> bool 
    // (const float* coordPair_lhs[], const float* coordPair_rhs[]) -> bool 
{
    return coordPair_lhs[1] < coordPair_rhs[1]; 
};
float** lhsColMinIt;
float** lhsColMaxIt;
// float* lhsColMinIt[2];
// float* lhsColMaxIt[2];
std::tie(lhsColMinIt, lhsColMaxIt) = std::minmax_element(pix, pix + sizeToParse, colLessThan);

我的所有尝试都因编译器错误而被拒绝 .

在接受的答案之后,它被简化为:

在实例化'std :: tuple <_T1,_T2>&std :: tuple <_T1,_T2> :: operator =(std :: pair <_U1,_U2> &&)[with _U1 = const float()[2 ] . _U2 = const float()[2]; _T1 = float(&)[2]; _T2 = float(&)[2]]':src / ClusterPairFunctions.cc:32:09:从这里需要/ data / hunyadi / usr / include / c /7.1.0/tuple:1252:25:错误:转换无效从'const float()[2]'到'float()[2]'[-fpermissive]

Update: 使用接受答案提供的方法,代码可以工作,我只是没有解释编译器在 std::tuple 内报告const不正确 .

1 回答

  • 2

    在C 14中,在lambda中使用 const auto& .

    如果必须明确提供类型:

    static auto colLessThan = [] (const float (&lhs)[2], const float (&rhs)[2])
    {
        return lhs[1] < rhs[1];
    };
    
    float (*lhsColMinIt)[2];
    float (*lhsColMaxIt)[2];
    std::tie(lhsColMinIt, lhsColMaxIt) =
        std::minmax_element(pix, pix + sizeToParse, colLessThan);
    

    Demo

相关问题