简体   繁体   English

无法正确执行 std::sort

[英]Unable to execute std::sort correctly

After doing some research I have resigned to ask what is going wrong with std::sort .在做了一些研究之后,我已经辞职询问std::sort出了什么问题。

Following Oliver Charlesworth advice from here I created a comparative, included the algorithm header and called std::sort in my function.按照这里的Oliver Charlesworth 建议,我创建了一个比较,包括算法头并在我的函数中调用std::sort I am still getting errors.我仍然收到错误。 Probably an oversight but still.可能是一个疏忽,但仍然如此。

Errors:错误:

Error C2780 void std::sort(_RanIt,_RanIt)': expects 2 arguments - 3 provided错误 C2780 void std::sort(_RanIt,_RanIt)':需要 2 个参数 - 提供 3 个

Error C2672 'std::sort': no matching overloaded function found错误 C2672“std::sort”:找不到匹配的重载函数

Error C3867 'ImageEvaluator::comparator': non-standard syntax;错误 C3867 'ImageEvaluator::comparator':非标准语法; use '&' to create a pointer to member使用“&”创建指向成员的指针

Struct:结构:

struct numLocWidth
{
    int width;
    int number;
    int location;
};

Comparative:比较:

bool comparator(const numLocWidth &a, const numLocWidth &b) 
{ 
    return a.location < b.location; 
}

Function:功能:

if (tempMax > minAcceptableValue)
{
    tempLocAndVal.location = max_Pot_Loc.x;
    tempLocAndVal.number = i - 1;
    tempLocAndVal.width = templates[7][i].size().width;
    foundNumbers.push_back(tempLocAndVal);
    std::sort(foundNumbers.begin(), foundNumbers.end(), comparator);                
}

Not sure what is going on here and have been scratching my head for a bit.不知道这里发生了什么,一直在挠我的头。 I could write my own sort function but I'm pretty sure this way will be more efficient.我可以编写自己的排序函数,但我很确定这种方式会更有效。

As VTT and Peter rightly pointed out, my comparative needed to be marked as static because it was a member of a class.正如 VTT 和 Peter 正确指出的那样,我的比较需要被标记为静态,因为它是一个类的成员。

More information on this can be found here under requirements and BinaryPredicates.可以在此处的要求和 BinaryPredicates 下找到有关这方面的更多信息。

Thanks for the help谢谢您的帮助

Solution.解决方案。 From:从:

bool comparator(const numLocWidth &a, const numLocWidth &b) 
{ 
    return a.location < b.location; 
}

To:到:

static bool comparator(const numLocWidth &a, const numLocWidth &b) 
{ 
    return a.location < b.location; 
}

Thanks again for the help再次感谢您的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM