delphi使用记录作为TDictionary中的键

r_j*_*r_j 14 delphi generics collections tdictionary

你能在TDictionary中使用记录作为Key值吗?我想基于字符串,整数和整数的组合来查找对象.

TUserParKey=record
  App:string;
  ID:integer;
  Nr:integer;
end;
Run Code Online (Sandbox Code Playgroud)

...

var
  tmpKey:TUserParKey;
  tmpObject:TObject;
begin
  tmpObject:= TTObject.Create(1); 
  tmpKey.App:='1';
  tmpKey.ID :=1;
  tmpKey.Nr :=1;

  DTUserPars.Add(tmpKey,tmpObject)
Run Code Online (Sandbox Code Playgroud)

...

var
  tmpKey:TUserParKey;
begin
  tmpKey.App:='1';
  tmpKey.ID :=1;
  tmpKey.Nr :=1;

  if not DTUserPars.TryGetValue(tmpKey,Result) then begin
    result := TTObject.Create(2); 
  end;
Run Code Online (Sandbox Code Playgroud)

这返回对象2.

Ste*_*nke 20

是的,你可以使用记录作为TDictionary中的键,但是你应该在创建字典时提供自己的IEqualityComparer,因为记录的默认值只是对记录进行了愚蠢的二进制比较.对于包含字符串的记录,这会失败,因为它只是比较该字符串的指针,即使该字符串包含相同的值,该指针也可能不同.

这样的比较器看起来像这样:

type
  TUserParKeyComparer = class(TEqualityComparer<TUserParKey>)
    function Equals(const Left, Right: TUserParKey): Boolean; override;
    function GetHashCode(const Value: TUserParKey): Integer; override;
  end;

function TUserParKeyComparer.Equals(const Left, Right: TUserParKey): Boolean;
begin
  Result := (Left.App = Right.App) and (Left.ID = Right.ID) and (Left.Nr = Right.Nr);
end;

function TUserParKeyComparer.GetHashCode(const Value: TUserParKey): Integer;
begin
  Result := BobJenkinsHash(PChar(Value.App)^, Length(Value.App) * SizeOf(Char), 0);
  Result := BobJenkinsHash(Value.ID, SizeOf(Integer), Result);
  Result := BobJenkinsHash(Value.Nr, SizeOf(Integer), Result);
end;
Run Code Online (Sandbox Code Playgroud)

  • 而不是`BobJenkinsHash(Value.App [1],Length(Value.App)*SizeOf(Char),0)`你可以使用`BobJenkinsHash(PChar(Value.App)^,Length(Value.App)*SizeOf( Char),0)`.它也适用于空字符串的情况. (3认同)