class
CStudent :
public
CBase
... {
public:
static CStudent* NewLC(const TDesC& aName, TInt aNum);
static CStudent* NewL(const TDesC& aName, TInt aNum);
TInt GetNum() const ...{ return iNum; }
const TDesC& GetName() const ...{ return *iName; }
CStudent& operator=(const CStudent& aStu);
static TInt GetNumOffset() ...{ return _FOFF(CStudent, iNum); }
static TInt CompareByNum(const CStudent& aStu1, const CStudent& aStu2);
~CStudent();
private:
CStudent(TInt aNum);
void ConstructL(const TDesC& aName);
private:
HBufC* iName;
TInt iNum;
} ;
CStudent * CStudent::NewLC( const TDesC & aName, TInt aNum)
... {
CStudent* self = new(ELeave) CStudent(aNum);
CleanupStack::PushL(self);
self->ConstructL(aName);
return self;
}
CStudent * CStudent::NewL( const TDesC & aName, TInt aNum)
... {
CStudent* self = NewLC(aName, aNum);
CleanupStack::Pop(self);
return self;
}
CStudent::CStudent(TInt aNum) : iNum(aNum) ... { }
void CStudent::ConstructL( const TDesC & aName)
... {
iName = aName.Alloc();
}
TInt CStudent::CompareByNum( const CStudent & aStu1, const CStudent & aStu2)
... {
if (aStu1.GetNum() == aStu2.GetNum())
return 0;
else
return aStu1.GetNum() > aStu2.GetNum() ? 1 : -1;
}
CStudent & CStudent:: operator = ( const CStudent & aStu)
... {
iNum = aStu.GetNum();
delete iName;
iName = aStu.GetName().Alloc();
return *this;
}
CStudent:: ~ CStudent()
... {
delete iName;
iName = NULL;
}
void Test()
... {
_LIT(KTom, "Tom");
_LIT(KJack, "Jack");
_LIT(KMary, "Mary");
_LIT(KJohn, "John");
CStudent* tom = CStudent::NewLC(KTom, 21);
CStudent* jack = CStudent::NewLC(KJack, 33);
CStudent* mary = CStudent::NewLC(KMary, 11);
CStudent* john = CStudent::NewLC(KJohn, 45);
const TInt KGranularity = 5; // 粒度
CArrayFixFlat<CStudent>* arr = new(ELeave)CArrayFixFlat<CStudent>(KGranularity);
CleanupStack::PushL(arr);
arr->AppendL(*tom);
arr->AppendL(*jack);
arr->AppendL(*mary);
arr->AppendL(*john);
// Before Sort
for (TInt i = 0; i < arr->Count(); ++i)
...{
console->Printf(arr->At(i).GetName());
console->Printf(_L(" %d "), arr->At(i).GetNum());
}
// Sort by Number
// 1).构造适当键的一个实例,指定字段偏移量、排序类型(用于char数组)和文本长度
// 2).传递键,在数组上调用Sort()
TKeyArrayFix key(CStudent::GetNumOffset(), ECmpTInt);
User::LeaveIfError( arr->Sort(key) );
// After Sort
for (TInt i = 0; i < arr->Count(); ++i)
...{
console->Printf(arr->At(i).GetName());
console->Printf(_L(" %d "), arr->At(i).GetNum());
}
// Find a element
// 1).构造适当键类型的一个实例,同时指定匹配字段的偏移量、比较类型以及文本长度
// 2).构造一个新对象,该对象包含所寻找的数据
// 3).在数组上调用Find(),同时传递键、实例对象和用于接受搜索结果的TInt。如果找到
// 相应的项,则Find()返回0,并且将搜索结果设置为找到项的索引。如果没有找到相
// 应的项,则将搜索结果设置为数组的大小,并且返回一个错误码
CStudent* goal = CStudent::NewL(KJack, 33);
TInt foundAt = 0;
TInt err = arr->Find(*goal, key, foundAt);
if (foundAt != arr->Count()) // Found
console->Printf(_L("Found at : %d "), foundAt);
else
console->Printf(_L("Not found! "));
delete goal;
arr->Reset(); // Reset Array
CleanupStack::PopAndDestroy(arr);
CleanupStack::PopAndDestroy(4, tom);
}
... {
public:
static CStudent* NewLC(const TDesC& aName, TInt aNum);
static CStudent* NewL(const TDesC& aName, TInt aNum);
TInt GetNum() const ...{ return iNum; }
const TDesC& GetName() const ...{ return *iName; }
CStudent& operator=(const CStudent& aStu);
static TInt GetNumOffset() ...{ return _FOFF(CStudent, iNum); }
static TInt CompareByNum(const CStudent& aStu1, const CStudent& aStu2);
~CStudent();
private:
CStudent(TInt aNum);
void ConstructL(const TDesC& aName);
private:
HBufC* iName;
TInt iNum;
} ;
CStudent * CStudent::NewLC( const TDesC & aName, TInt aNum)
... {
CStudent* self = new(ELeave) CStudent(aNum);
CleanupStack::PushL(self);
self->ConstructL(aName);
return self;
}
CStudent * CStudent::NewL( const TDesC & aName, TInt aNum)
... {
CStudent* self = NewLC(aName, aNum);
CleanupStack::Pop(self);
return self;
}
CStudent::CStudent(TInt aNum) : iNum(aNum) ... { }
void CStudent::ConstructL( const TDesC & aName)
... {
iName = aName.Alloc();
}
TInt CStudent::CompareByNum( const CStudent & aStu1, const CStudent & aStu2)
... {
if (aStu1.GetNum() == aStu2.GetNum())
return 0;
else
return aStu1.GetNum() > aStu2.GetNum() ? 1 : -1;
}
CStudent & CStudent:: operator = ( const CStudent & aStu)
... {
iNum = aStu.GetNum();
delete iName;
iName = aStu.GetName().Alloc();
return *this;
}
CStudent:: ~ CStudent()
... {
delete iName;
iName = NULL;
}
void Test()
... {
_LIT(KTom, "Tom");
_LIT(KJack, "Jack");
_LIT(KMary, "Mary");
_LIT(KJohn, "John");
CStudent* tom = CStudent::NewLC(KTom, 21);
CStudent* jack = CStudent::NewLC(KJack, 33);
CStudent* mary = CStudent::NewLC(KMary, 11);
CStudent* john = CStudent::NewLC(KJohn, 45);
const TInt KGranularity = 5; // 粒度
CArrayFixFlat<CStudent>* arr = new(ELeave)CArrayFixFlat<CStudent>(KGranularity);
CleanupStack::PushL(arr);
arr->AppendL(*tom);
arr->AppendL(*jack);
arr->AppendL(*mary);
arr->AppendL(*john);
// Before Sort
for (TInt i = 0; i < arr->Count(); ++i)
...{
console->Printf(arr->At(i).GetName());
console->Printf(_L(" %d "), arr->At(i).GetNum());
}
// Sort by Number
// 1).构造适当键的一个实例,指定字段偏移量、排序类型(用于char数组)和文本长度
// 2).传递键,在数组上调用Sort()
TKeyArrayFix key(CStudent::GetNumOffset(), ECmpTInt);
User::LeaveIfError( arr->Sort(key) );
// After Sort
for (TInt i = 0; i < arr->Count(); ++i)
...{
console->Printf(arr->At(i).GetName());
console->Printf(_L(" %d "), arr->At(i).GetNum());
}
// Find a element
// 1).构造适当键类型的一个实例,同时指定匹配字段的偏移量、比较类型以及文本长度
// 2).构造一个新对象,该对象包含所寻找的数据
// 3).在数组上调用Find(),同时传递键、实例对象和用于接受搜索结果的TInt。如果找到
// 相应的项,则Find()返回0,并且将搜索结果设置为找到项的索引。如果没有找到相
// 应的项,则将搜索结果设置为数组的大小,并且返回一个错误码
CStudent* goal = CStudent::NewL(KJack, 33);
TInt foundAt = 0;
TInt err = arr->Find(*goal, key, foundAt);
if (foundAt != arr->Count()) // Found
console->Printf(_L("Found at : %d "), foundAt);
else
console->Printf(_L("Not found! "));
delete goal;
arr->Reset(); // Reset Array
CleanupStack::PopAndDestroy(arr);
CleanupStack::PopAndDestroy(4, tom);
}