RArray 练习

RArray 练习

RArray 是一个动态数组,可以添加 T 类或 R类对像,对像添加到 RArray后,与原对像分开,RArray 保存的是原对像的一个副本
改变 RArray 中的对像值,原对像的值不会发生变化
下面是练习用到的代码

ExpandedBlockStart.gif 代码
_LIT(KTextConsoleTitle,  " Console " );
_LIT(KTextFailed, 
"  failed, leave code = %d " );
_LIT(KTextPressAnyKey, 
"  [press any key]\n " );
_LIT(Kenter,
" \n====\n " );

//   Global Variables
LOCAL_D CConsoleBase *  console;   //  write all messages to this
class  TStudent
{
public :
 TStudent(TDesC
&  aName,TInt aAge,TInt aScore)
 {
  iName 
=  aName;
  iAge  
=  aAge;
  iScore 
=  aScore;
 }
 
~ TStudent()
 {
 }
 
void  ToString(CConsoleBase *  aConsole)
 {   
  _LIT(KToString,
" name=%S \nAge=%d\nScore=%d\n " );
  
  aConsole
-> Printf(KToString, & iName,iAge,iScore);
 }
 
void  SetName(TDesC &  aName)
 {
  iName 
=  aName;
 }
 
const  TDesC &  GetName()  const
 {
  
return  iName;
 }
 
const  TInt GetScore()  const
 {
  
return  iScore;
 }
 
static  TBool CompareStudentScore( const  TStudent &  stu1, const  TStudent &  stu2)
 {
  
return  stu1.GetName().Compare(stu2.GetName()) == 0 ? ETrue:EFalse;
 }
 
static  TInt OrderStudentScore( const  TStudent &  stu1, const  TStudent &  stu2)
 {
  
if  (stu1.GetScore() > stu2.GetScore())
   
return   1 ;
  
else
  
if  (stu1.GetScore() < stu2.GetScore())
   
return   - 1 ;
  
else
   
return   0 ;
 }
protected :
private :
 TBuf
< 20 >  iName;
 TInt     iAge;
 TInt     iScore;
 
};

//   Local Functions
LOCAL_C  void  MainL( const  TDesC &  aArgs)
    {
    
//
    
//  add your program code here, example code below
    
//
    
// console->Write(_L("Hello, world!\n"));
 RArray < TStudent >  aList;
 _LIT(KQi,
" qijianzhou " );
 _LIT(KLiu,
" liumingsha " );
 TBuf
< 20 >  aQi(KQi);
 TBuf
< 20 >  aLiu(KLiu);
 TStudent stuQi(aQi,
12 , 34 );
 TStudent stuLiu(aLiu,
34 , 23 );
 stuLiu.ToString(console);
 stuQi.ToString(console);
 console
-> Printf(Kenter);
 CleanupClosePushL(aList);
 aList.AppendL(stuQi);
 aList.AppendL(stuLiu);
 
for  (TInt i = 0 ;i < aList.Count();i ++ )
 {
  aList[i].ToString(console);
 }
 
//  修改数据
 _LIT(KNewName, " Lixiao " );
 TBuf
< 20 >  temp(KNewName);
 aList[
0 ].SetName(temp);
 console
-> Printf(Kenter);
 aList[
0 ].ToString(console);
 console
-> Printf(Kenter);
 TInt iPos 
=  aList.Find(stuQi);
 _LIT(KNPos,
" Pos is:%d " );
 console
-> Printf(KNPos,iPos);
 console
-> Printf(Kenter);
 
//  添加到 RArray 后,好像和外面的对像就失去联系了
 
//  如下面的显示  第0位的 ToString 及 stuQi 的 ToString
 aList[ 0 ].ToString(console);
 stuQi.ToString(console);
 console
-> Printf(Kenter);
    
 
//  另外一个查找
 _LIT(KTemp, " Lixia4o " );
 TBuf
< 20 >  tempLiu(KTemp);
 TStudent t(tempLiu,
20 , 34 );
 TIdentityRelation
< TStudent >  rela(TStudent::CompareStudentScore);
 console
-> Printf(Kenter);
 iPos 
=  aList.Find(t,rela);
 console
-> Printf(KNPos,iPos);
 console
-> Printf(Kenter);
 
//  排序前
  for  ( i = 0 ;i < aList.Count();i ++ )
 {
  aList[i].ToString(console);
 }
 TLinearOrder
< TStudent >  aOrder(TStudent::OrderStudentScore);
 aList.Sort(aOrder);
 console
-> Printf(Kenter);
 
for  ( i = 0 ;i < aList.Count();i ++ )
 {
  aList[i].ToString(console);
 }
 console
-> Printf(Kenter);
 aList.Reset();
 CleanupStack::PopAndDestroy();

    console
-> Printf(_L( " Command line args: \ " % S\ " \n " ),  & aArgs);
    }

 


对于 RArray 的总结:

  1. RArray 的创建  RArray<类名> 变量名; 创建后要通过 CleanupClosePushL() 添加到清除栈中
  2. 添加操作 AppendL ,防止出现异常,所以用 L,添加一个对像到 RArray 后,RArray 保存的对像是原对像的一个副本,
    相互之间不会产生任何影响
  3. 通过 Count() 得到 RArray 目前的数量,然后可以通过 for 循环得到每个对像
  4. 可以通过 [] 得到每一个对像
  5. 可以通过 find 查找,find 查找的第二种形式是定义一个 TIdentityRelation 对像,该对像引用一个用于比较的方法
  6. 查找时要传入一个类型的对像,而排序时,则只传入 TLinearOrder 对像即可
  7. 用完毕后要通过 Reset()释放动态元素占用的空意,然后使用 CleanupStack::PopAndDestory() 从清除栈中清理

 



安平2009@原创
qi_jianzhou@126.com

转载于:https://www.cnblogs.com/zziss/articles/1641817.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值