我在排序自定義類指針列表時遇到問題。我需要排序的類是事件。這些被分配一個隨機時間,我需要按照正確的順序進行。C++在列表中排序自定義對象
#include <list>
Class Event{
public:
float time; // the value which I need to sort them by
int type; // to indicate which event i'm dealing with
Event(float tempTime, int tempType)
{
time = tempTime;
type = tempType;
}
int main(){
std::list<Event*> EventList;
list<Event*>::iterator it;
.........
如果你能幫我解決這個問題,將不勝感激!我一直堅持這幾個小時。
謝謝!
你的意思是'std :: list <> :: sort'。你不能在'std :: list'上使用'std :: sort',因爲它沒有隨機訪問迭代器。 – 2013-05-12 12:58:18
我曾嘗試使用自定義比較器功能,但它不適用於我。 (並使用list.sort)j – user2374868 2013-05-12 13:01:53
另外,重載'operator <'將無助於排序指針。 – 2013-05-12 13:02:45