gpt4 book ai didi

c++ - std::sort 仿函数声明

转载 作者:太空宇宙 更新时间:2023-11-04 15:17:49 26 4
gpt4 key购买 nike

这是我的源代码的样子(简化):

class UncertaintyTest
{
private:
class SpectralPeak
{
};

std::list<SpectralPeak*> peaks;
};

struct SpectralPeakComparator
{
bool operator()(const UncertaintyTest::SpectralPeak &a, const UncertaintyTest::SpectralPeak &b)
{
return a.TMiddleAvg() < b.TMiddleAvg();
}
};

void UncertaintyTest::SortSpectralPeaks()
{
std::sort(peaks.begin(), peaks.end(), SpectralPeakComparator());
}

编译器拒绝编译此源代码并出现以下错误:

 C2676: binary '-' : 'std::_List_iterator<_Mylist>' does not define this operator or a conversion to a type acceptable to the predefined operator   c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm    3868    1   Periodogram
C2780: 'void std::_Sort(_RanIt,_RanIt,_Diff)' : expects 3 arguments - 4 provided c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: ''unknown-type' std::operator -(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: ''unknown-type' std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: ''unknown-type' std::operator -(std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'std::move_iterator<_RanIt> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: 'std::complex<_Other> std::operator -(const _Ty &,const std::complex<_Other> &)' : could not deduce template argument for 'const std::complex<_Other> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: 'std::complex<_Other> std::operator -(const std::complex<_Other> &,const _Ty &)' : could not deduce template argument for 'const std::complex<_Other> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram
C2784: 'std::complex<_Other> std::operator -(const std::complex<_Other> &,const std::complex<_Other> &)' : could not deduce template argument for 'const std::complex<_Other> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 Periodogram

有问题的行是 std:sort。如果我评论它,一切都很好。我对比较器对象的声明有什么问题?

最佳答案

问题是双重的:

首先,排序仿函数不接受正确的值类型。您的列表包含指针,而不是直接包含对象,因此比较仿函数也必须使用指针:

struct SpectralPeakComparator
{
bool operator()(const UncertaintyTest::SpectralPeak *a, const UncertaintyTest::SpectralPeak *b)
{
return a->TMiddleAvg() < b->TMiddleAvg();
}
};

其次,std::list 没有 std::sort 需要的随机访问迭代器。您可以改为使用 std::list::sort:

peaks.sort(SpectralPeakComparator());

关于c++ - std::sort 仿函数声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27797240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com