声明std :: sort仿函数

时间:2015-01-06 11:00:15

标签: c++ stl visual-studio-2012

这是我的源代码的外观(简化):

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。如果我发表评论,一切都好。我的比较器对象声明有什么问题?

1 个答案:

答案 0 :(得分:7)

问题是双重的:

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

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());