排序指针列表 [英] sorting list of pointers

查看:53
本文介绍了排序指针列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用std :: list;


struct Something {

Something(val):value(val){}

bool operator<(Something& s){返回值< s.value; }

int value;

};


main(){

list<东西*>东西;

things.pushBack(new Something(10));

things.pushBack(new Something(20));

东西。 sort();

}


如何强制thing.sort()使用Something :: operator<(Something&)

而不是默认运算符<指针?

using std::list;

struct Something{
Something( val ): value( val ){}
bool operator<( Something & s ){ return value < s.value; }
int value;
};

main(){
list< Something * > things;
things.pushBack( new Something( 10 ) );
things.pushBack( new Something( 20 ) );
things.sort();
}

How to force things.sort() to use Something::operator<( Something & )
instead of default operator< for pointers ?

推荐答案

Nenad Jalsovec写道:
Nenad Jalsovec wrote:
使用std :: list;

struct Something {
Something(val):value(val){}
bool operator<(Something& s){return value< s.value; }
int value;
};

main(){
list<东西*>东西;
things.pushBack(new Something(10));
things.pushBack(new Something(20));
things.sort();
}

如何强制things.sort()使用Something :: operator<(Something&)
而不是默认运算符<指针?
using std::list;

struct Something{
Something( val ): value( val ){}
bool operator<( Something & s ){ return value < s.value; }
int value;
};

main(){
list< Something * > things;
things.pushBack( new Something( 10 ) );
things.pushBack( new Something( 20 ) );
things.sort();
}

How to force things.sort() to use Something::operator<( Something & )
instead of default operator< for pointers ?




struct Something_ptr_cmp

{

bool operator(Something * lhs,Something * rhs)

{

return * lhs< * rhs;

}

};


// ...


things.sort(Someting_ptr_cmp);



struct Something_ptr_cmp
{
bool operator(Something* lhs, Something* rhs)
{
return *lhs < *rhs;
}
};

//...

things.sort(Someting_ptr_cmp);




" Rolf Magnus" < RA ****** @ t-online.de>在消息中写道

news:cf ************* @ news.t-online.com ...

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cf*************@news.t-online.com...
Nenad Jalsovec写道:
Nenad Jalsovec wrote:
使用std :: list;

struct Something {
Something(val):value(val){}
bool operator< ;(Something& s){返回值< s.value; }
int value;
};

main(){
list<东西*>东西;
things.pushBack(new Something(10));
things.pushBack(new Something(20));
things.sort();
}

如何强制things.sort()使用Something :: operator<(Something&)
而不是默认运算符< for pointers?
struct Something_ptr_cmp
{bool operator(Something * lhs,Something * rhs)
using std::list;

struct Something{
Something( val ): value( val ){}
bool operator<( Something & s ){ return value < s.value; }
int value;
};

main(){
list< Something * > things;
things.pushBack( new Something( 10 ) );
things.pushBack( new Something( 20 ) );
things.sort();
}

How to force things.sort() to use Something::operator<( Something & )
instead of default operator< for pointers ?
struct Something_ptr_cmp
{
bool operator(Something* lhs, Something* rhs)




你忘了<了吗? ;在那里,如:


bool operator<(Something * lhs,Something * rhs)

{
return * lhs< * rhs;
}
};

// ...
things.sort(Someting_ptr_cmp);



Did you forget the < there, as in:

bool operator <(Something* lhs, Something* rhs)
{
return *lhs < *rhs;
}
};

//...

things.sort(Someting_ptr_cmp);



Howard写道:
Howard wrote:
" Rolf Magnus" < RA ****** @ t-online.de>在消息中写道
新闻:cf ************* @ news.t-online.com ...
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cf*************@news.t-online.com...
Nenad Jalsovec写道:
Nenad Jalsovec wrote:
>使用std :: list;
>
> struct Something {
> Something(val):value(val){}
> bool operator<(Something& s){返回值< s.value; }
> int值;
> };
>
> main(){
>列表<东西*>事情;
> things.pushBack(new Something(10));
> things.pushBack(new Something(20));
> things.sort();
> }
>
>
>
>如何强制things.sort()使用Something :: operator<(Something&
>)
>而不是默认运算符< for pointers?
> using std::list;
>
> struct Something{
> Something( val ): value( val ){}
> bool operator<( Something & s ){ return value < s.value; }
> int value;
> };
>
> main(){
> list< Something * > things;
> things.pushBack( new Something( 10 ) );
> things.pushBack( new Something( 20 ) );
> things.sort();
> }
>
>
>
> How to force things.sort() to use Something::operator<( Something &
> )
> instead of default operator< for pointers ?



struct Something_ptr_cmp
{bool operator(Something * lhs,Something * rhs)



struct Something_ptr_cmp
{
bool operator(Something* lhs, Something* rhs)



你忘了<在那里,如:



Did you forget the < there, as in:




不,我没有。但是我忘记了额外的事情。应该是:


bool operator()(某事* lhs,Something * rhs)


想法是有一个比较函数对象可以是

被叫 (这意味着它的operator()被调用)

算法。



No, I didn''t. But I forgot extra parens. Should be:

bool operator()(Something* lhs, Something* rhs)

The idea is to have a comparison function object that can be
"called" (which means its operator() gets called) by the sort
algorithm.


这篇关于排序指针列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆