当我在比较中有大数字时,TArray.Sort不起作用的原因是什么?
我的代码如下(Delphiy Tokyo):
Interface
Type
RCInd = record
Num : Integer;
Ger : Integer;
Confirmed : Boolean;
Total : Real;
End;
TArrInd = TArray<RCInd>;
Procedure SortInd (Var PArrayInd : TArrInd);
Implementation
Procedure SortInd (Var PArrayInd : TArrInd);
begin
TArray.Sort<RCInd>( PArrayInd,TComparer<RCInd>.Construct
( function (Const Rec1, Rec2 : RCInd) : Integer
begin
Result := - ( Trunc(Rec1.Total) - Trunc(Rec2.Total) );
end )
);
end;
......
Run Code Online (Sandbox Code Playgroud)
当Rec1.Total和Rec2.Total的值为整数限制时,此排序工作正常,但是当值超过整数限制时排序过程不起作用!它在PArrayInd中生成一组未排序的数据.
有谁帮我理解这里发生了什么?请提前!
我正在使用 Delphi Rio,我的程序有很多动态数组操作。为了提高一些长数组复制的速度,我尝试使用 Move 。对于基本类型(实数、整数)的一维动态数组,我可以管理 Move 的使用,但对于以记录作为其元素的动态数组,并且该记录具有另一个动态数组字段,我很难使其工作。我对动态数组记录字段使用 MOVE 的方式,在发出命令后,这些字段继续指向源数组的原始源地址。
测试过程:array_A是动态记录数组,该记录的两个字段都是动态数组。发出 Move(array_A[0],B_array[0],sizeof(Array[0]) * length(arra_A)) 后,然后更改 array_B 动态数组字段中的值,这也会导致 array_A 的值发生变化!两者都指向同一个地址。如何使用 MOVE 更改此行为?
看我的代码:
program ProjArrayMove;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
Type
Arrayint = Tarray<Integer>;
Arrayreal = TArray<Real>;
RECmember = record
mcode : integer;
mname : string[30];
mdynarr_int : arrayint;
mdynarr_real : arrayreal;
end;
ArrayMember = array of RECMember;
Procedure FillsRecord (var pRec : RECmember; pSize : integer; pFactor : real);
// Given a pRec , fills each dynamic array fields with …Run Code Online (Sandbox Code Playgroud) 我需要创建一些iTasks,它们将在不同的位置填充相同的数组.由于要为每个Task执行的代码是相同的,我决定创建一个iTasks数组并创建4个任务.将参数传递给iTask内的主要程序时出现问题.当我使用变量作为参数时,只考虑创建的最后一个任务的值.当我将参数作为值传递(硬编码)时,它会尊重每个任务的所有值.请看我的代码:
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
UNTThreads, Vcl.StdCtrls,
System.Threading ;
type
Vet = array of integer;
type
TFMThreadArray = class(TForm)
EDTArraySize: TEdit;
EDTNumberofThreads: TEdit;
Memo1: TMemo;
LBArraySize: TLabel;
LBThreads: TLabel;
BTUsingForLoop: TButton;
EDTThread: TEdit;
BTHardCoded: TButton;
procedure BTUsingForLoopClick(Sender: TObject);
procedure BTHardCodedClick(Sender: TObject);
private
{ Private declarations }
procedure ProcA ( Const pin, pfin, Psize, Ptask : integer;
Var Parray : vet);
public
{ Public declarations }
end;
var
FMThreadArray: TFMThreadArray;
implementation
{$R *.dfm}
// Procedure …Run Code Online (Sandbox Code Playgroud)