对对象列表进行排序 [英] Sorting a list of objects

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

问题描述

你好,


我正在完成一个语音识别点唱机

http://intelligentjukebox.com/ )我有一个非常基本的模板

问题。我找不到任何我想做的好例子。我可以用比b ++模板库中的例子更好的方式编写自己的排序函数。


有人能告诉我如何定义<运算符所以我可以使用

排序函数作为列表模板。我想用m_filename按照
az(降序)顺序对Song对象进行排序。


谢谢


这是我的代码:

// ------------------------------------- -----------------------------------------


class Song

{

public:

Album * m_pAlbum; //轻松获取专辑

string m_name; //显示名称

string m_filename;

};


list< Song *> * pSongsList; //保存这张专辑的所有歌曲


// --------------------------- -------------------------------------------------- -


我希望这个函数调用工作:


pSongsList-> sort();

Hello,

I''m finishing a speech recognition jukebox
(http://intelligentjukebox.com/) I have a very basic template
question. I can''t find any good examples of what I want to do. I
could write my own sort function faster than I could find a good
reference with examples on the c++ template library.

Can someone tell me how to define the "<" operator so I can use the
sort function for the list template. I want to sort Song objects in
a-z (descending) order by m_filename.

thanks

Here is my code:
//------------------------------------------------------------------------------

class Song
{
public:
Album* m_pAlbum; // make it easy to get the album
string m_name; //display name
string m_filename;
};

list<Song*> *pSongsList; // holds all the songs for this album

//------------------------------------------------------------------------------

I want to this function call to work:

pSongsList->sort();

推荐答案

Jeff写道:
Jeff wrote:
你好,

我正在完成一次语音识别自动点唱机
http://intelligentjukebox.com/ )我有一个非常基本的模板
问题。我找不到任何我想做的好例子。我可以更快地编写自己的排序函数,而不是在c ++模板库中找到一个很好的参考资料。

有人可以告诉我如何定义< ;"运算符,所以我可以使用
排序函数的列表模板。我想用m_filename按照z / az(降序)顺序对Song对象进行排序。

谢谢

这是我的代码:
// --- -------------------------------------------------- -------------------------

班级歌曲
{
公开:
专辑* m_pAlbum; //轻松获取专辑
string m_name; //显示名称
字符串m_filename;
};

list< Song *> * pSongsList; //保存这张专辑的所有歌曲

// ------------------------------- -----------------------------------------------

我希望这个函数调用工作:

pSongsList-> sort();
Hello,

I''m finishing a speech recognition jukebox
(http://intelligentjukebox.com/) I have a very basic template
question. I can''t find any good examples of what I want to do. I
could write my own sort function faster than I could find a good
reference with examples on the c++ template library.

Can someone tell me how to define the "<" operator so I can use the
sort function for the list template. I want to sort Song objects in
a-z (descending) order by m_filename.

thanks

Here is my code:
//------------------------------------------------------------------------------

class Song
{
public:
Album* m_pAlbum; // make it easy to get the album
string m_name; //display name
string m_filename;
};

list<Song*> *pSongsList; // holds all the songs for this album

//------------------------------------------------------------------------------

I want to this function call to work:

pSongsList->sort();




你可以'' t,因为该函数调用将使用运算符<在元素上

类型,它是指向Song的指针。你不能改变

指针的内置行为,因此你不能重载那个运算符<。

你可以做的是指定另一种比较:
< br $>
struct song_cmp

{

bool operator()(Song * lhs,Song * rhs)const

{

返回lhs-> m_filename< rhs-> m_filename;

}

};


pSongList.sort(song_cmp());



You can''t, because that function call would use operator< on the element
type, which is pointer to Song. You cannot change the built-in behavior of
pointers, therefore you cannot overload that operator<.
What you can do is specify an alternative comparison:

struct song_cmp
{
bool operator()(Song* lhs, Song* rhs) const
{
return lhs->m_filename < rhs->m_filename;
}
};

pSongList.sort(song_cmp());


2005年8月17日星期三01:06:13 +0200,Rolf Magnus< ra ****** @ t-online.de>

写道:
On Wed, 17 Aug 2005 01:06:13 +0200, Rolf Magnus <ra******@t-online.de>
wrote:

你不能,因为该函数调用将使用运算符<在元素
类型,这是指向宋的指针。你不能改变
指针的内置行为,因此你不能重载那个运算符<。
你可以做的是指定另一种比较:

struct song_cmp {
bool operator()(Song * lhs,Song * rhs)const
{
返回lhs-> m_filename< rhs-> m_filename;
}
};

pSongList.sort(song_cmp());

You can''t, because that function call would use operator< on the element
type, which is pointer to Song. You cannot change the built-in behavior of
pointers, therefore you cannot overload that operator<.
What you can do is specify an alternative comparison:

struct song_cmp
{
bool operator()(Song* lhs, Song* rhs) const
{
return lhs->m_filename < rhs->m_filename;
}
};

pSongList.sort(song_cmp());




我正在使用VC ++ 6.0。你能告诉我如何编译吗?

谢谢

----------------------- --------

第95行:

m_pSongsList-> sort(song_cmp());

编译......

music.cpp

J:\ home \ jeff\src\intelligent_jukebox\dev\intellimu sic \current\music.cpp(95 )

:错误C2664:''void __thiscall std :: list< class Song *,class

std :: allocator< class Song *> > :: sort(struct std :: greater< class Song

*>)'':无法将参数1从''

ct song_cmp''转换为''struct std :: greater< class Song *>''

没有构造函数可以采用源类型或构造函数

重载决策是不明确的



I''m using VC++ 6.0. Can you tell me how to make this compile?
thanks
-------------------------------
line 95:
m_pSongsList->sort(song_cmp());
Compiling...
music.cpp
J:\home\jeff\src\intelligent_jukebox\dev\intellimu sic\current\music.cpp(95)
: error C2664: ''void __thiscall std::list<class Song *,class
std::allocator<class Song *> >::sort(struct std::greater<class Song
*>)'' : cannot convert parameter 1 from ''stru
ct song_cmp'' to ''struct std::greater<class Song *>''
No constructor could take the source type, or constructor
overload resolution was ambiguous


在五分钟内,我写了一个冒泡排序,它为我的节目启动时间增加了不到1/10
秒的14,000首歌曲。大多数

专辑的歌曲少于20首。


我花了两个多小时试图找出延迟的乐谱

to排序列表并失败。显然没有明确的例子

如何使用您自己的比较函数对列表进行排序。有时我

真的很讨厌c ++。


感谢你的帮助,尽管


int n;

bool bSorted = false;

Song * tmpSong;


while(false == bSorted)

{

bSorted = true;

for(n = 0; n< m_totalSongs-1; n ++)

{

如果((m_pSongsArray [N]) - > m_filename>

(m_pSongsArray [N + 1]) - > m_filename)

{

//交换值

tmpSong = m_pSongsArray [N];

m_pSongsArray [N] = m_pSongsArray [N + 1];

m_pSongsArray [n + 1] = tmpSong;

bSorted = false;

}

}

} / /结束时

In five minutes I wrote a bubble sort which added less than 1/10 of a
second to the startup time of my program for 14,000 songs. Most
albums have less than 20 songs.

I spent more than two hours trying to figure out the retarded notation
to sort a list and failed. Apparently there are no clear examples of
how to sort a list using your own comparison function. Sometimes I
really hate c++.

thanks for the help anyway though

int n;
bool bSorted = false;
Song* tmpSong;

while (false == bSorted)
{
bSorted = true;
for (n = 0; n < m_totalSongs-1; n++)
{
if ( (m_pSongsArray[n])->m_filename >
(m_pSongsArray[n+1])->m_filename )
{
// swap values
tmpSong = m_pSongsArray[n];
m_pSongsArray[n] = m_pSongsArray[n+1];
m_pSongsArray[n+1] = tmpSong;
bSorted = false;
}
}
} // end while


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

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