Delphi的object Pascal里面有没有类似Map的数据结构,就是给定一个Key(String类型)可以找到对应的value。
例如
  value1:=map('key1');
  value2:=map('key2');
或者那位大侠已经实现了这种功能的类,也发出来共享一下吧

解决方案 »

  1.   

    TStringList 好象有这个功能。
      

  2.   

    Delphi没有这样的数据类型,但有一个THashedStringList可以考虑使用,但THashedStringList对于数据的检索功能并不强大。可以考虑使用第三方的一些Hash结构。
      

  3.   

    其实就是最基本的哈西表.任何数据结构的书里面都会讲到如何构建一个哈西表给你一个最简单的字符串的哈西表算法
    unit UnitHasedTable;interface
    uses
      Classes;type
      TStringHashedTable = class(TPersistent)
        FKeyList: TStrings;
        FStrList: TStrings;
      private
        function GetItems(Key: string): string;
        procedure SetItems(Key: string; const Value: string);
        function GetCount: Integer;
      public
        constructor Create;
        destructor Destroy; override;
        property Items[Key: string]: string read GetItems write SetItems; default;
        property Count: Integer read GetCount;
        procedure Add(Key, Str: string);
        procedure Delete(Key:String);  end;implementation{ TStringHashedTable }procedure TStringHashedTable.Add(Key, Str: string);
    begin
      if FKeyList.IndexOf(Key) <> -1 then
      begin    Exit;
      end;
      FKeyList.Add(Key);
      FStrList.Add(Str);
    end;constructor TStringHashedTable.Create;
    begin  FKeyList := TStringList.Create;
      FStrList := TStringList.Create;
    end;procedure TStringHashedTable.Delete(Key:String);
    var
      KeyIndex:Integer;
    begin
      KeyIndex := FKeyList.IndexOf(Key);
      FKeyList.Delete(KeyIndex);
      FStrList.Delete(KeyIndex);
    end;destructor TStringHashedTable.Destroy;
    begin
      FStrList.Free;
      FKeyList.Free;
      inherited Destroy;
    end;function TStringHashedTable.GetCount: Integer;
    begin
      Result := FKeyList.Count;
    end;function TStringHashedTable.GetItems(Key: string): string;
    var
      KeyIndex                 : Integer;
    begin
      KeyIndex  :=FKeyList.IndexOf(Key);
      Result := FStrList[KeyIndex];
    end;procedure TStringHashedTable.SetItems(Key: string; const Value: string);
    var
      KeyIndex                 : Integer;
    begin
      KeyIndex  :=FKeyList.IndexOf(Key);
      FStrList[KeyIndex] := Value;
    end;end.
    ------------------------------
    用法
    ------------------------------
    var
      FHT:TStringHashedTable;
      Msg:String;
    begin
      FHT:=TStringHashedTable.Create;
      FHT.Add('Key1','aaa');
      FHT.Add('Key2','aaabbb');
      FHT.Add('Key3','aaaccc');
      Msg:= FHT['Key3'];
      ShowMessage(Msg);
      FHT.Free;
    end;