std::sort 仿函数的声明
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());
这是我的源代码的样子(简化):
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());