A set is a collection of values of
the same ordinal type. The values have no inherent order, nor is it meaningful
for a value to be included twice in a set.
The range of a set type is the
power set of a specific ordinal type, called the base type; that is, the
possible values of the set type are all the subsets of the base type, including
the empty set. The base type can have no more than 256 possible values, and
their ordinalities must fall between 0 and 255. Any construction of the form
set of baseType
where baseType is an appropriate
ordinal type, identifies a set type.
Because of the size limitations
for base types, set types are usually defined with subranges. For example, the
declarations
type
TSomeInts = 1..250;
TIntSet = set of TSomeInts;
create a set type called TIntSet
whose values are collections of integers in the range from 1 to 250. You could
accomplish the same thing with
type
TIntSet = set of 1..250;
Given this declaration, you can
create a sets like this:
var Set1,
Set2: TIntSet;
...
Set1 := [1, 3, 5, 7, 9];
Set2 := [2, 4, 6, 8, 10]
You can also use the set of
... construction directly in variable declarations:
var
MySet: set of 'a'..'z';
...
MySet := ['a','b','c'];
Other examples of set types
include
set of
Byte
set of
(Club, Diamond, Heart, Spade)
set of
Char;
The in operator tests set
membership:
if 'a' in
MySet then ... { do something } ;
Every set type can hold the empty set, denoted by [].
集合是指相同序数类型值的集合。集合中的值没有固定的顺序,同一个值在一个集合中最多出现一次(即集合中的元素互不相同的)。
The range of a set type is the power set of a specific ordinal type, called the base type; that is, the possible values of the set type are all the subsets of the base type, including the empty set. The base type can have no more than 256 possible values, and their ordinalities must fall between 0 and 255. Any construction of the form
集合类型的范围是指定序数类型的幂集(见当前页中编者注),这里所说的“指定序数类型”叫做集合的基类型(base type)。也就是说,集合类型所有可能的值都是基类型的子集,包括空集。基类型不能含有多余256个可能的值,基类型的序号也必需在0到255之间。任何具有如下形式
set of baseType
(这里的基类型baseType是一个适当的序数类型),都标识了一个集合类型。
由于基类型的尺寸局限,集合类型通常用子界类型来定义。例如,声明
type
TSomeInts = 1..250;
TIntSet = set of TSomeInts;
创建了一个叫做TintSet的集合类型,集合元素的取值范围是1到250。也可以如下声明,达到相同效果
type
TIntSet = set of 1..250;
对于上面给出的声明,可以有如下代码:
var Set1,
Set2: TIntSet;//声明集合变量
...
Set1 := [1, 3, 5, 7, 9];//向集合赋值
Set2 := [2, 4, 6, 8, 10]//向集合赋值
也可以在变量声明中直接使用set of ... 结构:
var
MySet: set of 'a'..'z';
...
MySet := ['a','b','c'];
下面是其他集合类型的范例
set of
Byte
set of
(Club, Diamond, Heart, Spade)
set of
Char;
in运算符用于测试集合成员:
if 'a' in
MySet then ... { 相应的处理
} ;
所有的集合都能保存空集,用 [] 表示(一对方括号)。
所谓幂集,就是给定元素范围,可能构成的所有集合的集合。如,对于集合[1, 2, 3, 4],其幂集是
[[], [1], [2], [3], [4], [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]]
如果用M表示该幂集,对于下面的声明
S: set of 1..4;
那么,S的范围是M。
Object Pascal规定集合的基类型可能的值不能超过256个并且序号必需在0到255之间,这同时也表明,集合中元素的个数最多是256个;给定了声明的集合,如果最大元素个数为n,那么该集合的范围是2^n个不同集合,包括空集在内。