# Delphi集合

2589人阅读 评论(0)

//定义
type
Uppercase = 'A'..'Z';
Letters = set of Uppercase;  //基于子界定义

TBorderIcon = (biSystemMenu, biMinimize, biMaximize, biHelp);
TBorderIcons = set of TBorderIcon;  //基于枚举定义

//应用
type
Uppercase = 'A'..'Z';
Letters = set of Uppercase;  //基于子界定义
var
L1, L2, L3: Letters;
begin
L1 := ['A', 'B', 'C'];
L2 := ['K'];
L3 := [];

if 'A' in L1 then ShowMessage('true');  //true
if L2=['K'] then ShowMessage('true');  //true
if L3=[] then ShowMessage('true');  //true
end;

//判断是否属于集合
type
TSet = set of (A,B,C);
var
set1: TSet;
begin
set1 := [A,B];
if A in set1 then
ShowMessage('集合 set1 包含 A');
end;

//集合元素的增减
type
TSet = set of (A,B,C);
var
set1: TSet;
begin
set1 := [A];

set1 := set1 + [B];  //增加一个元素
Include(set1,B);     //增加一个元素

set1 := set1 - [B];  //排除一个元素
Exclude(set1,B);     //排除一个元素
end;

//集合添加元素举例
type
TCharSet = set of char;
var
character: 'a'..'z';
charSet: TCharSet;
word: string;
begin
charSet := [];
for character in [Low(character) .. High(character)] do
begin
if character in ['d', 'e', 'l', 'p', 'h', 'i'] then
begin
Include(charSet, character);
end;
end;

for character in charSet do
begin
word := word + character;
end;

ShowMessage(word);  //dehilp
end;


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{\$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
set1: set of Char; {定义一个字符集合变量}
c: Char;           {定义个字符用于遍历集合}
num: Integer;      {用于计数}
begin
set1 := ['a'..'z', 'A'..'Z', '0'..'9']; {给集合赋值}

num := 0;
for c in set1 do Inc(num); {遍历字符集合 set1, 让 num 计数}

ShowMessage(IntToStr(num)); {62}
end;

end.


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：2322047次
• 积分：24362
• 等级：
• 排名：第253名
• 原创：401篇
• 转载：102篇
• 译文：4篇
• 评论：1292条
博客专栏
 Delphi之重 文章：17篇 阅读：58931
最新评论