delphi - 将 TStack 代码从 Delphi 转换为 Lazarus

标签 delphi pascal lazarus

在 Delphi 中,我有以下代码,并且一切正常:

var
  StackOptions:TStack<String>;
  s:string;
  bfisio:boolean;
begin
  StackOptions:=TStack<String>.Create;
  //some pushs here 
  for s in StackOptions do begin
    dosomething;
  end;
end;

在 Lazarus 中我可以这样做:

uses
  ..., gstack;

type
  TStringStack = specialize TStack<String>;

var
  StackOptions: TStringStack;
  s:string;
begin
  //But this code doesn;t compile
  StackOptions := TStringStack.Create;
  //some pushs here 
  for s in StackOptions do begin // <-- Error 
    dosomething;
  end;
end;

我在 Lazarus 中收到下一个错误:

Compile Project, Target: TicketLaz.exe: Exit code 1, Errors: 1
umain.pas(263,12) Error: Cannot find an enumerator for the type "TStack$1$crcAC3AF268"

如何循环堆栈并使用 Lazarus 搜索值而不从堆栈中删除项目?

最佳答案

FPC 的堆栈由 TVector 支持。
TVector 有一个枚举器。

您可以轻松添加类助手,如下所示:
快速而肮脏的代码。

type
  TStringStack = specialize TStack<String>;

 type

   { TStackHelper }

   TVectorEnumerator = specialize TVector<string>.TVectorEnumerator;

   TStackHelper = class helper for TStringStack
     function GetEnumerator: TVectorEnumerator;
   end;

{ TStackHelper }

function TStackHelper.GetEnumerator: TVectorEnumerator;
begin
  Result:= FData.GetEnumerator;
end;

我真的不明白为什么堆栈不应该有迭代器。
即使在组装中,您也可以简单地执行 mov reg,[esp-04] .
这种清教徒式的数据结构方法对任何人都没有帮助

由于 TStack 是通用的,所有这一切都变得复杂。
我知道 FPC 允许通用类助手,但我不确定如何使该解决方案适用于所有人 TStack<T>

另一种方法是简单地编辑 gstack.pas暴露迭代器。

关于delphi - 将 TStack 代码从 Delphi 转换为 Lazarus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35901870/

相关文章:

string - 删除 Pascal 字符串中的短词

Delphi:弹出菜单

string - 对数字字符串求和

德尔福XE2 : TListView as tile view not working in Windows XP

scripting - 嵌入帕斯卡

if-statement - if 语句和 Pascal 中的最大值

linux - 我可以在哪里或如何将我的代码挂接到 TDaemonApplication 上的 Unix 信号(主要是 SIGHUP)?

conditional - 如何使 {$IFNDEF DEBUG} 在 lazarus/osx 项目中工作

java - JNI AttachCurrentThread 使 jenv 为空

delphi - 我应该使用什么来以编程方式在 Delphi 中生成打印输出/报告