[英]Declaration of std::sort functor
這是我的源代碼的外觀(簡化):
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());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.