C++ Builder中使用System.Generics.Collections
来源:互联网 发布:模考软件 编辑:程序博客网 时间:2024/05/21 17:00
本文以TList<T>为例介绍如何在C++Builder中使用System.Generics.Collections提供的类及方法,在C++Builder中使用System.Generics.Collections分两种情况。
1、对于基本数据类型C++Builder,使用System.Generics.Collections与DELPHI没有什么差别。例如TList<int>,使用在使用单元前要首先引用System.Generics.Collections.hpp头文件。
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#include <System.Generics.Collections.hpp>/*这个头文件必须引用*/
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
在这个例子中我定义了一个TList<int>类型的数据,定义编译后正常。向TList<int> 列表中添加内容与DELPHI的区别是DELPHI直接调用TList<T>的Add方法。而C++Builder是调用的TList<T>Items的Add成员函数。而定义TList<int>C++Builder与DELPHI是有区别, DELPH直接定义为TList<int> ,而C++Builder定义为TList__1<int>现将C++Builder的程序代码复制如下:
void __fastcall TForm2::Button1Click(TObject *Sender)
{
TList__1<int> *ilist=new TList__1<int>(); //声明一下TList<int>变量
for (int i = 0; i < 10; i++) { //向TList添加成员
ilist->Add(i);
}
ilist->Remove(8);//移除一个TList<int>成员,其中移除指定成员
ilist->Delete(0);//移除TList<int>中指定索引成员,该例子是删除ilist列表的第一个成员
ilist->Clear();//删除ilist中的所有成员
}
按照上面的程序写法编译不会出现问题,若是非基本数据类型的列表,在C++Builder中使用会出现编译错误:例如若想定义一个String列表那么在C++Builder中会编译错误。
是不是在C++Builder中,非基本数据类型就不能使用TList<T>的呢。答案是肯定。那么如何能让非基本数据类型使用TList呢,需要有使用前创建一个DELPHI程序单元,在DLPHI程序单元中定义一个类,定义格式如下:
type
TSlist=class(TList<string>)
end;
并将此这个程序单元引用到使用TList<String>数据类型的单元中,引用后的程序头文件如下:
这样就解决了TList列表不能在C++Builder使用的问题。依次类推System.Generics.Collections的其他几个泛型数据如
TDictionary等也可以用这种方式解决。
- C++ Builder中使用System.Generics.Collections
- System.Collections 学习中。。。
- Collections and Generics
- C#:关于System.Collections空间
- Java Generics and Collections [ILLUSTRATED]
- Section 16 Collections and Generics
- C++Builder中使用Excel的类
- [C++] BC++ Builder 中使用 Cookie
- C++Builder中TIniFile类的使用
- 如何在C++Builder中使用ACE
- 如何在C Builder中使用全局变量
- 如何在C++Builder中使用全局变量
- C++Builder中使用Pas文件
- c++builder 中关于#pragma使用整理
- 为什么要使用using System.Collections.Generic;?
- System.Collections
- 《程序员的第一年》---------- C#中System.Collections.Generic.SortedDictionary 的使用
- C++Builder使用经验谈
- 探空火箭气动参数仿真
- Hadoop MapReduce原理(推荐)
- easyui messager
- three.js 源码注释(十七)Math/Math.js
- 开发人生系列之-前言
- C++ Builder中使用System.Generics.Collections
- JS面向对象编程之对象
- Bitmap 转成Drawable
- LeetCode——Rotate List
- CocoaPods安装和使用教程
- Java重要资源汇总,记录一下。
- xml文件 字符串解析
- ios之开发中常用的设计模式介绍
- Failed to request gap sequence