2016-03-19 87 views
1

我有一个用户定义的类的向量,我想对这个向量进行排序。将会有3个属性基于哪个排序可以发生。我正在使用lambda函数来捕获基于哪个向量应该排序的属性。但是,我收到编译器错误。我粘贴下面的代码:用lambda函数排序stl容器

mapNode * PhotonMap::createTree(int start, int end) 
{ 
    mapNode *n = new mapNode(); 

    if (end - start == 0) 
    { 
     n->node.sPosition.setVector(pMap[end].sPosition); 
     n->node.pPower.setVector(pMap[end].pPower); 
     n->sAxis = -1; 
     n->left = NULL; 
     n->right = NULL; 
     return n; 
    } 

    BBox box; 

    if (!(end - start + 1 == pMap.size())) 
     box.setBBox(computeBox(start, end)); 

    int splitAxis = decodeSplitAxis(box.getMaxSpreadAxis()); 
    n->sAxis = splitAxis; 

    std::sort(pMap[start], pMap[end], [splitAxis](Photon &first, Photon &second) ->bool { return (first.sPosition.getValue(splitAxis) > second.sPosition.getValue(splitAxis)); }); 

    int mIndex = floor((start + end)/2); 

    n->node.sPosition.setVector(pMap[mIndex].sPosition); 
    n->node.pPower.setVector(pMap[mIndex].pPower); 

    if (mIndex == start) 
    { 
     //this means end - start = 1. There will be no left node! 
     n->left = NULL; 
     n->right = createTree(mIndex + 1, end); 
     return n; 
    } 
    else { 
     n->left = createTree(start, mIndex); 
     n->right = createTree(mIndex + 1, end); 
     return n; 
    } 

} 

我正的错误有以下几种:

错误C2784:“未知类型的std ::运营商 - (标准:: move_iterator < _RanIt> & ,常量的std :: move_iterator < _RanIt2> &) ':不能推导出模板参数为 '的std :: move_iterator < _RanIt> &' 从 '光子'

错误C2784:' 未知类型的std ::运营商 - (缺点牛逼的std :: reverse_iterator的< _RanIt> &,常量的std :: reverse_iterator的< _RanIt2> &)“:不能推导出模板参数的 '常量的std :: reverse_iterator的< _RanIt> &' 从 '光子'

错误C2676:二进制“ - ”:“光子”不限定此运算符或转换到类型接受的预定义的运算符

错误C2672:“_Sort”:没有匹配的重载函数发现

错误C2780:“无效STD :: _ Sort(_RanIt,_RanIt,_Diff,_Pr)':期望4个参数 - 3提供

Photonstruct。它的声明是:

typedef struct Photon { 

    Vector sPosition; 
    Vector iDirection; 
    Vector pPower; 
    Vector norm; 

} Photon; 

Vectorclass,其私有成员:

int length; 
void allocateMemory(void); 
float vector[3]; 

回答

1

前两个参数std::sort()应该是迭代器,在容器中的内容不引用。指针也可以工作,所以:

std::sort(&pMap[start], &pMap[end], /* your lambda goes here */); 

既然你没有提供Minimum, Complete, and Verifieable example,也可能有其他问题与您的代码,以防止编译,但是这至少是他们的第一个。