RArray 练习
RArray 练习
RArray 是一个动态数组,可以添加 T 类或 R类对像,对像添加到 RArray后,与原对像分开,RArray 保存的是原对像的一个副本
改变 RArray 中的对像值,原对像的值不会发生变化
下面是练习用到的代码
代码
_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 的总结:
- RArray 的创建 RArray<类名> 变量名; 创建后要通过 CleanupClosePushL() 添加到清除栈中
- 添加操作 AppendL ,防止出现异常,所以用 L,添加一个对像到 RArray 后,RArray 保存的对像是原对像的一个副本,
相互之间不会产生任何影响 - 通过 Count() 得到 RArray 目前的数量,然后可以通过 for 循环得到每个对像
- 可以通过 [] 得到每一个对像
- 可以通过 find 查找,find 查找的第二种形式是定义一个 TIdentityRelation 对像,该对像引用一个用于比较的方法
- 查找时要传入一个类型的对像,而排序时,则只传入 TLinearOrder 对像即可
- 用完毕后要通过 Reset()释放动态元素占用的空意,然后使用 CleanupStack::PopAndDestory() 从清除栈中清理
安平2009@原创
qi_jianzhou@126.com