c++中list.sort()不能指定自己的排序规则

suyongsheng suyongsheng 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
class MY
{
public:
bool operator() (int _X, int _Y) const
{
return _X > _Y;
}


};


void main( )
{
using namespace std;
list<int> v1;
list<int>::iterator Iter1;

for (int i = 0 ; i <= 5 ; i++ )
{
v1.push_back(i) ;
}

//v1.sort(greater<int>( )); //这个可以
v1.sort(MY( )); //自己指定的不可以
// sort(v1.begin( ), v1.end( ), MY()); //若把list换成vector,然后运行这句又可以了

}

错误提示:F:\My Documents\Test\main.cpp(39) : error C2664: 'void __thiscall std::list<int,class std::allocator<int> >::sort(struct std::greater<int>)' : cannot convert parameter 1 from 'class MY' to 'struct std::greater<int>'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.



list.sort()到底能不能指定自己的排序规则?求高手指点啊
...全文

相关推荐

评论 19
收藏 已收藏
分享
返回顶部
suyongsheng
C++ 语言 65,214 成员 250,514 内容