结构化数组和搜索
我需要一些建议来解决这个问题。我有这个数据结构:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一个唯一的错误,
myvalue
变量的类型应该是PMyArray
,而不是QmyArray
。下次遇到此类问题时,请尝试使用TArray.BinarySearch
的长手写版本,以便 Code Insight 实际上显示有意义的类型名称。当您编写时:
编译器需要从参数猜测数组的类型。如果
myarray
和myvalue
的类型不匹配,编译器将无法判断您真正想要什么,并且代码洞察无法真正向您显示它想要什么。但是您可以通过使用以下语法告诉它要操作的数组类型来帮助它:通过此语法,编译器知道您打算处理
PmyArray
数组。代码洞察也知道这一点,并向您显示它所需的确切参数类型。这有效:
You have one sole error, the type of the
myvalue
variable should bePMyArray
, notQmyArray
. Next time you're facing such a problem try using the long-hand version ofTArray.BinarySearch
, so that Code Insight actually shows meaningful type names.When you write:
the compiler needs to guess the type of your array from the parameters. If you missmatch the types of
myarray
andmyvalue
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: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: