RPointArray 练习

RPointArray 练习

RPointArray 与 RArray 不同在于  RPointArray 保存的是指针,如果改变 RPointArray 中的元素值,则通过 Append 到
RPointArray 的对像的值也会改变,因为改变的都是同一对像
查找函数与排序函数与 RArray 是一样的

以下是练习的代码,没有查找函数

代码
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;
 }
 
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"));
 RPointerArray<TStudent> aList;
 CleanupClosePushL(aList);
 _LIT(KLilei,
"LiLei");
 _LIT(KxiaoNing,
"xiaoNing");
 _LIT(Kxiaoxiao,
"xiaoxiao");
 TBuf
<20> BufLilei(KLilei);
 TBuf
<20> BufXiaoxiao(Kxiaoxiao);
 TBuf
<20> BufXN(KxiaoNing);
 TStudent 
*T1 = TStudent::NewL(BufLilei,20,80);
 TStudent 
*T2 = TStudent::NewL(BufXiaoxiao,20,82);
 TStudent 
*T3 = TStudent::NewL(BufXN,20,70);
 T1
->ToString(console);
 
/**/
 aList.AppendL(T1);
 aList.AppendL(T2);
 aList.AppendL(T3);
 TInt i;
 
for(i=0;i<aList.Count();i++)
  aList[i]
->ToString(console);
 
////
 // 改变第一个的 name
 _LIT(Kqi,"jianzhou");
 TBuf
<20> BufQi(Kqi);
 aList[
0]->SetName(BufQi);
 aList[
0]->ToString(console);
 T1
->ToString(console);

 
for(i=0;i<aList.Count();i++)
  aList[i]
->ToString(console);
 TLinearOrder
<TStudent> ord1(TStudent::Order);
 aList.Sort(ord1);
 
for(i=0;i<aList.Count();i++)
  aList[i]
->ToString(console);
 aList.Reset();
 CleanupStack::PopAndDestroy();
    console
->Printf(_L("Command line args: \"%S\"\n"), &aArgs);
    }

 

 

 



安平2009@原创
qi_jianzhou@126.com

posted @ 2010-01-09 22:30  zziss  阅读(284)  评论(0)    收藏  举报