📄 tutogeneriques.dpr
字号:
program TutoGeneriques;
{$APPTYPE CONSOLE}
uses
Types,
SysUtils,
Classes,
TypInfo,
Generics.Defaults,
Generics.Collections,
Generics.Trees in 'Generics.Trees.pas',
Generics.CollectionsEx in 'Generics.CollectionsEx.pas',
Generics.Nullable in 'Generics.Nullable.pas';
procedure WriteSquares(Max: Integer);
var
List: TList<Integer>;
I: Integer;
begin
List := TList<Integer>.Create;
try
for I := 0 to Max do
List.Add(I*I);
for I := 0 to List.Count-1 do
WriteLn(Format('%d*%0:d = %d', [I, List[I]]));
finally
List.Free;
end;
end;
function DistanceToCenterSquare(const Point: TPoint): Integer; inline;
begin
Result := Point.X*Point.X + Point.Y*Point.Y;
end;
function DistanceToCenter(const Point: TPoint): Extended; inline;
begin
Result := Sqrt(DistanceToCenterSquare(Point));
end;
type
TPointComparer = class(TComparer<TPoint>)
function Compare(const Left, Right: TPoint): Integer; override;
end;
function TPointComparer.Compare(const Left, Right: TPoint): Integer;
begin
Result := DistanceToCenterSquare(Left) - DistanceToCenterSquare(Right);
end;
procedure SortPointsWithTPointComparer;
const
MaxX = 100;
MaxY = 100;
PointCount = 10;
var
List: TList<TPoint>;
I: Integer;
Item: TPoint;
begin
List := TList<TPoint>.Create(TPointComparer.Create);
try
for I := 0 to PointCount-1 do
List.Add(Point(Random(2*MaxX+1) - MaxX, Random(2*MaxY+1) - MaxY));
List.Sort; // utilise le comparateur pass
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -