In pascal, a set is composed of a set of data elements with the same ordered type . This ordered type is called the base type of the set.
Some people think that the collections in Delphi are useless. These people include myself, but in fact they are not. It also has many application scenarios. For example, in the past, many elements were always taken out of the database in the program, and then judged one by one. Now using collections can completely avoid this operation
Definition and description
Basic syntax: set of base type;
The base type can be any order type, but cannot be a real type or other structure type. At the same time, the sequence number of the base type data must not exceed 255 . Because the subrange type and the enumeration type are both ordered, they can be used together, as follows:
function IsContainCharacter(Character: Char): Boolean;
var
Chars: set of 'A' .. 'Z';
begin
Result := Character in Chars;
end;
begin
try
Writeln(IsContainCharacter('B'));
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
Note: Because the number of elements in the set does not exceed 256, var s:set of integer; is wrong.
Element traversal
procedure ListCharacters();
var
Chars: set of char;
Str: string;
begin
Chars := ['A' .. 'J', 'a', 'm'];
for var C in Chars do begin
Str := Str + C;
end;
Writeln(Str);
end;
begin
ListCharacters;
Readln;
end.
Other operations
type
TEnum = (One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten);
TSetEnum = set of TEnum;
//初始化
procedure TForm4.Button3Click(Sender: TObject);
begin
SetEnum := [one, Two];
SetEnum1 := [Three, Nine, Ten]
end;
//集合减少
procedure TForm4.Button5Click(Sender: TObject);
begin
SetEnum := SetEnum - [Two];
end;
//集合增加
procedure TForm4.Button6Click(Sender: TObject);
begin
SetEnum := SetEnum + [Three];
end;
//集合减少
procedure TForm4.Button7Click(Sender: TObject);
begin
Exclude(SetEnum, One);
end;
//集合增加
procedure TForm4.Button4Click(Sender: TObject);
begin
Include(SetEnum, Four);
Include(SetEnum, Ten);
end;
Although the operation of the collection elements can be achieved through the +-symbol, the recommended usage is to use Include and Exclude as much as possible.