对地图进行排序< T *> [英] Sorting a map<T*>
问题描述
我有一个地图< T *,用于存储指向对象的指针。我如何告诉map< T *到
使用对象''运算符<()而不是指针的值进行排序?
如果是这样的话这是不可行的,我有什么替代方法可以对一组对象进行排序
不能也不应该被复制(即我只能使用指向这些对象的指针
对象)?
注意:我知道创建一个新类的方法,该类包装对象的
指针,然后提供自己的运算符<( )。但是,我试图避免创建新类。
I have a map<T*that stores pointers to objects. How can I tell map<T*to
use the objects'' operator<() and not the value of the pointers for sorting?
If that''s not feasible, what alternatives do I have to sort a set of objects
that cannot and should not be copied (i.e. I can use only pointers to these
objects)?
Note: I am aware of the method of creating a new class that wraps the
pointers of the objects and then provides its own operator<(). However, I
am trying to avoid creating new classes.
推荐答案
barcaroller写道:
barcaroller wrote:
我有一个地图< T *,用于存储指向对象的指针。如何告诉map< T *到
使用对象''运算符<()而不是指针的值进行排序?
I have a map<T*that stores pointers to objects. How can I tell map<T*to
use the objects'' operator<() and not the value of the pointers for sorting?
没有地图< T *>。 map需要(至少)两个模板参数 -
键和值。
There is no map<T*>. map takes (at least) two template parameters --
the key and the value.
如果这不可行,那么替代方法我必须对一组对象进行排序
不能也不应该被复制(即我只能使用指向这些
对象的指针)?
注意:我知道创建一个新类的方法,该类包装对象的
指针,然后提供自己的运算符<()。但是,我试图避免创建新类。
If that''s not feasible, what alternatives do I have to sort a set of objects
that cannot and should not be copied (i.e. I can use only pointers to these
objects)?
Note: I am aware of the method of creating a new class that wraps the
pointers of the objects and then provides its own operator<(). However, I
am trying to avoid creating new classes.
为什么?如果它能完成这项工作吗?
Why? If it does the job?
" red floyd" < no ***** @ here.dudewrote in message
news:EB ***************** @ newssvr23.news.prodigy.ne t ...
"red floyd" <no*****@here.dudewrote in message
news:EB*****************@newssvr23.news.prodigy.ne t...
没有地图< T *>。 map需要(至少)两个模板参数 -
键和值。
There is no map<T*>. map takes (at least) two template parameters --
the key and the value.
对不起;我的错。我的意思是设置< T *>。
I''m sorry; my mistake. I meant set<T*>.
5月7日下午3:01,barcaroller < barcarol ... @ music.netwrote:
On May 7, 3:01 pm, "barcaroller" <barcarol...@music.netwrote:
我有一个< T *来存储指向对象的指针。我如何告诉set< T * to
使用对象''运算符<()而不是指针的值进行排序?
I have a set<T*that stores pointers to objects. How can I tell set<T*to
use the objects'' operator<() and not the value of the pointers for sorting?
有一种方法可以创建一个包含两个参数的集合:set< MyType,
key_comp>,其中
key_comp是您定义的键比较函数。例如,
set< int,less< int。
那么你只需提供相应的比较函数
使用
对象''运算符<(),适当包装。
Michael
There''s a way to create a set with two parameters: set<MyType,
key_comp>, where
key_comp is a key comparison function you define. For example,
set<int, less<int.
So then you''d just need to provide the appropriate comparison function
that uses
the objects'' operator<(), suitably wrapped.
Michael
这篇关于对地图进行排序< T *>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!