#include <iostream>
#include <iomanip> // 內含 I/O 格式控制器
#include <stdlib.h>
#include <time.h>
#include <CXXLLIST.HPP>
using namespace std;
using namespace CxxlMan;
class A:virtual public cxxlObject
{
int m_i;
public:
A(int i)
:cxxlObject(Spirit_Urgent)
{
m_i = i;
}
virtual ~A()
{
}
int Get() const
{
return m_i;
}
};
// 提供由小至大排序 A 物件的 list
class MyList:public cxxlList<A>
{
// 有用到 Clone() 時,用此來產生延伸類別的物件
// 所以延伸類別必須覆載
MyList * cxxlFASTCALL CreateSelf(ISpirit *spirit) const
{
return new MyList;
}
// 使用排序須覆寫這個函數以符合自己的須求
bool cxxlFASTCALL Diagnosis(const A *P, const A *N) const
{
return (P->Get() < N->Get())? true:false;
}
public:
};
int
main(int argc, char
* argv[])
{
Smart_Ptr<cxxlList<A> > A_List1(new MyList);
cout << "產生 10 組亂數放到 A_list1 中:" << endl;
srand((unsigned)time(NULL));
for(int i = 0;i < 10; i++)
{
int n = rand()%100;
A_List1->Push_Back(new A(n));
}
Smart_Ptr<cxxlList<A> > A_List2 = A_List1->Clone(); // 複製
A_List2->Sort(); // 啟動排序
A_List1->ResetPT(toHead);
for(Smart_Ptr<A> A_Ptr = (*A_List1)++; !A_Ptr.isNULL(); A_Ptr = (*A_List1)++)
{
cout << setw(2) << setfill('0') << A_Ptr->Get() << " ";
}
cout << endl << "A_List2 的內容:" << endl;
A_List2->ResetPT(toHead);
for(Smart_Ptr<A> A_Ptr = (*A_List2)++; !A_Ptr.isNULL(); A_Ptr = (*A_List2)++)
{
cout << setw(2) << setfill('0') << A_Ptr->Get() << " ";
}
cout << endl;
system("pause");
}