CArrayFixFlat 练习

CArrayFixFlat 练习

CArrayFixFlat 属于 CArray 动态数组,该数组的每个元素拥有相同的长度,支持删除,添加,及插入操作
查询及排序,下面是练习的代码


 

代码
class TStudent
{
public:
 
static TStudent* NewL(TDesC& aName,TInt aAge,TInt aScore) 
 {
  TStudent
* self = TStudent::NewLC(aName,aAge,aScore);
  CleanupStack::Pop();
  
return self;
 }
 
static TStudent* NewLC(TDesC& aName,TInt aAge,TInt aScore) 
 {
  TStudent
* self = new(ELeave) TStudent();
  CleanupStack::PushL(self);
  self
->ConstructL(aName,aAge,aScore);
  
return self;
 }
 TStudent()
 {

 }
 TStudent(TDesC
& aName,TInt aAge,TInt aScore)
 {
  
this->ConstructL(aName,aAge,aScore);
 }
 
void ToString(CConsoleBase* aConsole)
 {
  _LIT(KTmp,
"Name=%S;Age=%d\nScore=%d\n");
  
  aConsole
->Printf(KTmp,&iName,iAge,iScore);
 }
 TDesC
& GetName()
 {
  
return iName;
 }
 
void SetName(TDesC& aName)
 {
  iName 
= aName;
 }
 
const TInt& GetScore() const
 {
  
return iScore;
 }
 
 
static TInt Order(const TStudent& t1,const TStudent& t2) 
 {
  
if (t1.GetScore()>t2.GetScore())
  {
   
return 1;
  } 
else
   
if (t1.GetScore()<t2.GetScore())
   {
    
return -1;
   } 
else
    
return 0;
 }
 
private:
 TBuf
<20> iName;
 TInt iAge;
 TInt iScore;
protected:
 
void ConstructL(TDesC& aName,TInt aAge,TInt aScore)
 {
  iName 
= aName;
  iAge  
= aAge;
  iScore 
= aScore;
 }
 
};

 

LOCAL_C 
void MainL(const TDesC& aArgs)
    {
    
//
    
// add your program code here, example code below
    
//
    
//console->Write(_L("Hello, world!\n"));
    CArrayFixFlat<TStudent>* arrayFlat;
 arrayFlat 
= new(ELeave)CArrayFixFlat<TStudent>(5);
 _LIT(KLilei,
"AAA");
 _LIT(KxiaoNing,
"BBB");
 _LIT(Kxiaoxiao,
"CCC");
 TBuf
<20> BufLilei(KLilei);
 TBuf
<20> BufXiaoxiao(Kxiaoxiao);
 TBuf
<20> BufXN(KxiaoNing);
 TStudent
* T1 = TStudent::NewL(BufLilei,20,80);
 TStudent
* T2 = TStudent::NewL(BufXN,20,82);
 TStudent
* T3 = TStudent::NewL(BufXiaoxiao,20,70);
 
 
/*
 TStudent T1 = TStudent(BufLilei,20,80);
 TStudent T2 = TStudent(BufXN,20,82);
 TStudent T3 = TStudent(BufXiaoxiao,20,70);
 
 
*/
 arrayFlat
->AppendL(*T1);
 arrayFlat
->AppendL(*T2);
 arrayFlat
->AppendL(*T3);
 _LIT(KTmp,
"temp");
 TBuf
<20> tmp (KTmp);
 (
*arrayFlat)[0].SetName(tmp);
 (
*arrayFlat)[0].ToString(console);
 T1
->ToString(console);
 console
->Getch();
 console
->ClearScreen();
 TInt i;
 
for (i=0;i<arrayFlat->Count();i++)
  (
*arrayFlat)[i].ToString(console);
 
// 删除第一个元素
 console->Getch();
 console
->ClearScreen();
// arrayFlat->Delete(1);
 for (i=0;i<arrayFlat->Count();i++)
  (
*arrayFlat)[i].ToString(console);
 console
->Getch();
 console
->ClearScreen();
 
//for(i=arrayFlat->Count()-1;i>=0;i--)
 
// arrayFlat->Delete(i);
 
// 查找
 _LIT(KSearch,"CCC");
 TBuf
<20> bufSearch(KSearch);
 TStudent s1(bufSearch,
20,82);
 TKeyArrayFix nameKey(_FOFF(TStudent,GetName()),ECmpNormal);
 TInt findPos;
 _LIT(KFormatPos,
"Pos=%d\nnotFind=%d\n");
 
if (arrayFlat->Find(s1,nameKey,findPos) == 0)
 {
  console
->Printf(KFormatPos,findPos,0);
 
// (*arrayFlat)[findPos].ToString(console);
 }
 
else
  console
->Printf(KFormatPos,-1,-1);
 
 
/*
 查找 find 返回 0 代表找到,否则没有找到,找到后 把 索引值赋给
 findPos (也就是第三个参数),通过索引可以得到该元素
 
*/

 
// 排序
 TKeyArrayFix sortKey(_FOFF(TStudent,GetScore()),ECmpTInt32);
 User::LeaveIfError(arrayFlat
->Sort(sortKey));
 
for(i=0;i<arrayFlat->Count();i++)
  (
*arrayFlat)[i].ToString(console);

 
// 插入操作 
 console->Getch();
 console
->ClearScreen();
 _LIT(KDDD,
"DDD");
 TBuf
<20> ddd(KDDD);
 TStudent d(ddd,
22,90);
 arrayFlat
->InsertL(1,d);
 
for(i=0;i<arrayFlat->Count();i++)
  (
*arrayFlat)[i].ToString(console);

 delete arrayFlat;  
// 回收并释放资源
    console->Printf(_L("\nCommand line args: \"%S\"\n"), &aArgs);
    }

 

 

CArrayFixFlat 用于元素大小固定的情况,以下是对其总结

  1. 初始化时,要将其初始化成指针,经查看帮助,发现重载了 new 操作符,返回的都是指针
  2. 不需要将其添加到清除栈中,在使用完毕后,通过 delete 来删除这个指针对像
  3. 添加元素时调用 AppendL ,因为 CArray 可能会产生异常
  4. 删除元素时调用 delete(索引) 来删除指定的索引元素
  5. 插入时调用 InsertL 方法
  6. 查找时需要定义一个 key ,对于CArrayFixFlat 来说是 TKeyArrayFix ,使用方式是
    TKeyArrayFix aKey(类名,类的公过属性);
    调用 find 传入一个要查找的元素,一个 TKeyArrayFix 对像,及返回的索引值
    这个 find 如果找到,则返回 0 ,否则返回非零值,找到后,传入 find 的第三个
    参数为该元素的索引,注意:如果有多个相同的元素,则只返回第一个元素的位置
  7. 排序时用 sort ,同样也需要一个 TKeyArrayFix 对像,同 find 不同的是,sort 只
    需要一个 TKeyArrayFix 对像
  8. 添加到该动态数组后,与原对像脱离关系,这个只是原对像的一份拷贝

CArrayFixFlat 与 RArray 大部分使用方法相同,保存的元素是原对像的一份拷贝,所不同的是
CArrayFixFlat 拥有删除方法,而RArray及RPointArray 没有删除方法,CArrayPtrFlat 保存的是
对像的指针,与RPointArray一样。

posted @ 2010-01-11 23:19  zziss  阅读(585)  评论(0编辑  收藏  举报