关于CArrayPtrFlat 的查找与排序

关于CArrayPtrFlat 的查找与排序

搜遍了网络也没有找到关于 CArrayPtrFlag 查找与排序,上论坛发贴,也没有找到解决的方法,好像没有人去使用这个功能,最近初学手机开发
想把基础打好,所以一步一步的学习,当学到 CArrayPtrFlat 时,发现在查找时需要用 TKeyArrayFix 的派生类去实现,在 SDK 中也没有找
到。功夫不负有心人,终于在书中找到了,但是不是很具体,后来找到源代码,真正解决。其实代码为什么要这么写我也没有弄清楚,作者说
不要去理解这些,用时只需要把代码拿过来就行了,以下是代码

class TKeyArrayPtr:public TKeyArrayFix
{
public:
 inline TKeyArrayPtr(TInt anOffset,TKeyCmpText aType):TKeyArrayFix(anOffset,aType)
 {}
 inline TKeyArrayPtr(TInt aOffset, TKeyCmpText aType, TInt aLength) :
 TKeyArrayFix(aOffset, aType, aLength) {};
    inline TKeyArrayPtr(TInt aOffset, TKeyCmpNumeric aType) : TKeyArrayFix(aOffset, aType) {};
 virtual TAny* At(TInt anIndex) const
 {
  if (anIndex == KIndexPtr)
   return *(TUint8**)iPtr+iKeyOffset;
  else
  return *(TUint8**)iBase->Ptr(anIndex* sizeof(TUint8**)).Ptr()+iKeyOffset;
 }
 
};

 

附我练习时的代码

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;
 }
 
};
class TKeyArrayPtr:public TKeyArrayFix
{
public:
 inline TKeyArrayPtr(TInt anOffset,TKeyCmpText aType):TKeyArrayFix(anOffset,aType)
 {}
 inline TKeyArrayPtr(TInt aOffset, TKeyCmpText aType, TInt aLength) :
 TKeyArrayFix(aOffset, aType, aLength) {};
    inline TKeyArrayPtr(TInt aOffset, TKeyCmpNumeric aType) : TKeyArrayFix(aOffset, aType) {};
 virtual TAny* At(TInt anIndex) const
 {
  if (anIndex == KIndexPtr)
   return *(TUint8**)iPtr+iKeyOffset;
  else
  return *(TUint8**)iBase->Ptr(anIndex* sizeof(TUint8**)).Ptr()+iKeyOffset;
 }
 
};

LOCAL_C void MainL(const TDesC& aArgs)
    {
    //
    // add your program code here, example code below
    //
    //console->Write(_L("Hello, world!\n"));
    CArrayPtrFlat<TStudent>* arrayFlat;
 arrayFlat = new(ELeave)CArrayPtrFlat<TStudent>(2);
 _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,70);
 TKeyArrayPtr 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 (也就是第三个参数),通过索引可以得到该元素
 */
 // 排序
 TKeyArrayPtr 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);
 arrayFlat->ResetAndDestroy();
 // 不能这样释放吗?
 console->Getch();
 console->ClearScreen();
 _LIT(KCount,"count=%d");
 console->Printf(KCount,arrayFlat->Count());
 //for(i=0;i<arrayFlat->Count();i++)
//  (*arrayFlat)[i]->ToString(console);
 delete arrayFlat;  // 回收并释放资源
    console->Printf(_L("\nCommand line args: \"%S\"\n"), &aArgs);
    }

 

 



安平2009@原创
qi_jianzhou@126.com

posted @ 2010-01-12 23:51  zziss  阅读(400)  评论(0编辑  收藏  举报