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

suyongsheng 2009-12-15 03:03:03
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()到底能不能指定自己的排序规则?求高手指点啊
...全文
1061 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2009-12-16
  • 打赏
  • 举报
回复
呃,可怜人哪,都撞到南墙了还死撑着不肯回头。
suyongsheng 2009-12-15
  • 打赏
  • 举报
回复
是vc6.0,平时用着挺好用的,难道再这个小问题上挂掉了?
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 suyongsheng 的回复:]
感谢13楼。但是我用的是vc2006,编译时居然报错:f:\my documents\test\main.cpp(28) : error C2664: 'void __thiscall std::list <int,class std::allocator <int> >::sort(struct std::greater <int>)' : cannot convert parameter 1 from 'class myComp <int>' to 'struct std::greater <int>'
但是用同学的vc2008就可以。难道非要装vc2008啊,有没有解决办法啊!vc2008实在是也太大了,现在还不想装啊!
[/Quote]
VC貌似没有2006版啊
herman~~ 2009-12-15
  • 打赏
  • 举报
回复
MARK
herry-Li 2009-12-15
  • 打赏
  • 举报
回复
很负责人的告诉你,VC6.0对模板的支持很差 比如偏特化、萃取、元编程...........
所以,快换2008吧
suyongsheng 2009-12-15
  • 打赏
  • 举报
回复
感谢13楼。但是我用的是vc2006,编译时居然报错:f:\my documents\test\main.cpp(28) : error C2664: 'void __thiscall std::list<int,class std::allocator<int> >::sort(struct std::greater<int>)' : cannot convert parameter 1 from 'class myComp<int>' to 'struct std::greater<int>'
但是用同学的vc2008就可以。难道非要装vc2008啊,有没有解决办法啊!vc2008实在是也太大了,现在还不想装啊!
Dinelgua 2009-12-15
  • 打赏
  • 举报
回复

#include <list>
template<typename kk>
class myComp
{
public:
bool operator()(const kk& left,const kk& right)
{
return left > right;
}
};
int main()
{
std::list<int> iList;
list <int>::iterator c1_Iter;

iList.push_back( 20 );
iList.push_back( 10 );
iList.push_back( 30 );


for ( c1_Iter = iList.begin( ); c1_Iter != iList.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;

iList.sort(myComp<int>());

for ( c1_Iter = iList.begin( ); c1_Iter != iList.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;


return 0;
}

2009-12-15
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 suyongsheng 的回复:]
重载这个类的 <操作符确实可以。这里先谢谢大家。但是3楼的例子中c1.sort( greater <int>( ) );
通过传递一个函数也是可以的。如果用这种方式,又该怎么写代码。再麻烦一下大家了,只想把这个知识点弄的更清楚一点。
[/Quote]
写一个类,对其重载“函数调用运算符”。
class Cmp
{
public:
bool operator () (int, int); //例如对两个 int 进行比较
};

用的时候 c1.sort(Cmp());
suyongsheng 2009-12-15
  • 打赏
  • 举报
回复
重载这个类的<操作符确实可以。这里先谢谢大家。但是3楼的例子中c1.sort( greater<int>( ) );
通过传递一个函数也是可以的。如果用这种方式,又该怎么写代码。再麻烦一下大家了,只想把这个知识点弄的更清楚一点。
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 suyongsheng 的回复:]
楼上的这个看过了,现在时我想自己指定排序规则,应该怎么写?
[/Quote]
自己写一个规则呀。greater <int>( )就是一个规则
taodm 2009-12-15
  • 打赏
  • 举报
回复
珍惜生命,远离VC6
yshuise 2009-12-15
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
#include <list>
using namespace std;

class A{
public:
A(int m):a(m){}
bool operator <(const A& other)
{
return a < other.a;
}
friend std::ostream& operator <<(std::ostream& out, const A& obj){
out<< obj.a<<endl;
return out;
}
private:
int a;
};


int _tmain(int argc, _TCHAR* argv[])
{
list<A> S;
typedef list<A>::iterator it;
S.push_back(A(3));
S.push_back(A(2));
S.push_back(A(5));
S.sort();
it it1 = S.begin();
it it2 = S.end();
while(it1 != it2)
{
cout<<*it1<<endl;
it1++;
}
return 0;
}

2

3

5

请按任意键继续. . .
linuhuge 2009-12-15
  • 打赏
  • 举报
回复

class Nod
{
public:
int a;
int b;
bool operator < (const Nod& n)
{
return b < n.b;
}
};



int main(void)
{
list<Nod> ilist;
for (int i = 0; i < 10; ++i)
{
Nod tmp;
tmp.a = i;
tmp.b = 10-i;
ilist.push_back(tmp);
}

ilist.sort();

list<Nod>::iterator ite;
for (ite = ilist.begin(); ite != ilist.end(); ++ite)
{
cout << (ite->a) << " " << (ite->b) << endl;
}

return 0;
}


重载 < 就可以了。。
linuhuge 2009-12-15
  • 打赏
  • 举报
回复
class Nod
{
public:
int a;
int b;
bool operator < (const Nod& n)
{
return b < n.b;
}
};



int main(void)
{
list<Nod> ilist;
for (int i = 0; i < 10; ++i)
{
Nod tmp;
tmp.a = i;
tmp.b = 10-i;
ilist.push_back(tmp);
}

ilist.sort();

list<Nod>::iterator ite;
for (ite = ilist.begin(); ite != ilist.end(); ++ite)
{
cout << (ite->a) << " " << (ite->b) << endl;
}

return 0;
}
至善者善之敌 2009-12-15
  • 打赏
  • 举报
回复
一个是不带参数的sort(),用来实现升序排列;
另一个是带参数的sort(greater<T> pr),用来实现降序排列。
如果想自己制定排序规则,则相当于要重载sort了,所以楼主要是打算自己使用指针的话,也不是不可以:)
yshuise 2009-12-15
  • 打赏
  • 举报
回复
对operator < 进行重载
suyongsheng 2009-12-15
  • 打赏
  • 举报
回复
楼上的这个看过了,现在时我想自己指定排序规则,应该怎么写?
  • 打赏
  • 举报
回复
// list_sort.cpp
// compile with: /EHsc
#include <list>
#include <iostream>

int main( )
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter;

c1.push_back( 20 );
c1.push_back( 10 );
c1.push_back( 30 );

cout << "Before sorting: c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;

c1.sort( );
cout << "After sorting c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;

c1.sort( greater<int>( ) );
cout << "After sorting with 'greater than' operation, c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
}
  • 打赏
  • 举报
回复
可以

64,662

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧