结构化数组和搜索

发布于 2024-12-27 20:46:17 字数 1261 浏览 4 评论 0原文

我需要一些建议来解决这个问题。我有这个数据结构:

type
  QmyArray = array of integer;
  PmyArray = record
    element: Qmyarray;
    value: integer;
  end;
  TMyArray = array of Pmyarray;

var
  myarray: tmyArray;
  myvalue: qmyarray;

我正确设置了 myarray 的所有值,并且 myarray.element 的所有值都正确排序,这很好。我遇到的问题是当我想在 myarray 中搜索 myvalue 并获取值时。 对于搜索,我使用二进制搜索并写入:

tarray.BinarySearch(myarray, myvalue, index);

当然它不起作用。因为我知道需要自定义比较器,所以我写:

function CompareB(const Left, Right: integer): Integer;
begin
  if Left < Right then
    Result := -1
  else if Left > Right then
    Result := 1
  else
    Result := 0;
end;

function CompareA(const Left, Right: pmyarray): Integer;
var
  iIndex: Integer;
begin
  Result := CompareB(Left.element[0], Right.element[0]);
  for iIndex := 1 to High(Right.element) do
    if Result = 0 then
      Result := CompareB(Left.element[iIndex], Right.element[iIndex]);
end;

并尝试使用:

tarray.BinarySearch(myarray, myvalue, index, TComparer<Pmyarray>.Construct(CompareA));

但在这种情况下我收到此错误:

[DCC 错误] Project1.dpr(98): E2250 没有可以使用这些参数调用的“BinarySearch”的重载版本

,我不明白我在哪里犯了错误。

我该如何解决?

I need some suggestions for solving this problem. I have this data structure:

type
  QmyArray = array of integer;
  PmyArray = record
    element: Qmyarray;
    value: integer;
  end;
  TMyArray = array of Pmyarray;

var
  myarray: tmyArray;
  myvalue: qmyarray;

I set all values of myarray correctly, and all values of myarray.element are sorted correctly and this is fine. The problem I have is when I want search myvalue in myarray and get value.
For searching, I use binarysearch and write:

tarray.BinarySearch(myarray, myvalue, index);

and of course it's not working. Because I understood that need customized the comparer so I write:

function CompareB(const Left, Right: integer): Integer;
begin
  if Left < Right then
    Result := -1
  else if Left > Right then
    Result := 1
  else
    Result := 0;
end;

function CompareA(const Left, Right: pmyarray): Integer;
var
  iIndex: Integer;
begin
  Result := CompareB(Left.element[0], Right.element[0]);
  for iIndex := 1 to High(Right.element) do
    if Result = 0 then
      Result := CompareB(Left.element[iIndex], Right.element[iIndex]);
end;

and try with:

tarray.BinarySearch(myarray, myvalue, index, TComparer<Pmyarray>.Construct(CompareA));

but in this case I receive this error:

[DCC Error] Project1.dpr(98): E2250 There is no overloaded version of 'BinarySearch' that can be called with these arguments

and I don't understand where I made a mistake.

How can I solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2025-01-03 20:46:17

您有一个唯一的错误,myvalue 变量的类型应该是 PMyArray,而不是 QmyArray。下次遇到此类问题时,请尝试使用 TArray.BinarySearch 的长手写版本,以便 Code Insight 实际上显示有意义的类型名称。

当您编写时:

TArray.BinarySearch(myarray, myvalue, index, iComparer)

编译器需要从参数猜测数组的类型。如果 myarraymyvalue 的类型不匹配,编译器将无法判断您真正想要什么,并且代码洞察无法真正向您显示它想要什么。但是您可以通过使用以下语法告诉它要操作的数组类型来帮助它:

TArray.BinarySearch<PmyArray>(myarray, myvalue, index, iComparer)

通过此语法,编译器知道您打算处理 PmyArray 数组。代码洞察也知道这一点,并向您显示它所需的确切参数类型。

这有效:

program Project9;

{$APPTYPE CONSOLE}

uses
  SysUtils, Generics.Collections, Generics.Defaults;

type
  QmyArray = array of Integer;
  PmyArray = record
    element: QmyArray;
    value: Integer;
  end;
  TMyArray = array of PmyArray;

var myarray: TMyArray;
    myvalue: PmyArray;
    FoundIndex: Integer;

function CompareB(const Left, Right: integer): Integer;
begin
  if Left < Right then
    Result := -1
  else if Left > Right then
    Result := 1
  else
    Result := 0;
end;

function CompareA(const Left, Right: pmyarray): Integer;
var
  iIndex: Integer;
begin
  Result := CompareB(Left.element[0], Right.element[0]);
  for iIndex := 1 to High(Right.element) do
    if Result = 0 then
      Result := CompareB(Left.element[iIndex], Right.element[iIndex]);
end;

begin
  TArray.BinarySearch<PmyArray>(myarray, myvalue, FoundIndex, TComparer<PmyArray>.Construct(CompareA));
end.

You have one sole error, the type of the myvalue variable should be PMyArray, not QmyArray. Next time you're facing such a problem try using the long-hand version of TArray.BinarySearch, so that Code Insight actually shows meaningful type names.

When you write:

TArray.BinarySearch(myarray, myvalue, index, iComparer)

the compiler needs to guess the type of your array from the parameters. If you missmatch the types of myarray and myvalue the compiler can't tell what you really wanted and code insight has no chance of actually showing you what it wants. But you can give it a hand by telling it the type of array you want to operate on, using this syntax:

TArray.BinarySearch<PmyArray>(myarray, myvalue, index, iComparer)

With this, the compiler knows you intend to deal with an array of PmyArray. Code insight also knows that and shows you the exact types of parameters it needs.

This works:

program Project9;

{$APPTYPE CONSOLE}

uses
  SysUtils, Generics.Collections, Generics.Defaults;

type
  QmyArray = array of Integer;
  PmyArray = record
    element: QmyArray;
    value: Integer;
  end;
  TMyArray = array of PmyArray;

var myarray: TMyArray;
    myvalue: PmyArray;
    FoundIndex: Integer;

function CompareB(const Left, Right: integer): Integer;
begin
  if Left < Right then
    Result := -1
  else if Left > Right then
    Result := 1
  else
    Result := 0;
end;

function CompareA(const Left, Right: pmyarray): Integer;
var
  iIndex: Integer;
begin
  Result := CompareB(Left.element[0], Right.element[0]);
  for iIndex := 1 to High(Right.element) do
    if Result = 0 then
      Result := CompareB(Left.element[iIndex], Right.element[iIndex]);
end;

begin
  TArray.BinarySearch<PmyArray>(myarray, myvalue, FoundIndex, TComparer<PmyArray>.Construct(CompareA));
end.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文