Dav*_*nan 13
标准集合不提供通用集合类.第四方集合库,如Spring4D.
您可以轻松地构建一个通用的集合类TDictionary<K, V>.裸骨版本可能如下所示:
type
TSet<T> = class
private
FDict: TDictionary<T, Integer>;
public
constructor Create;
destructor Destroy; override;
function Contains(const Value: T): Boolean;
procedure Include(const Value: T);
procedure Exclude(const Value: T);
end;
....
constructor TSet<T>.Create;
begin
inherited;
FDict := TDictionary<T, Integer>.Create;
end;
destructor TSet<T>.Destroy;
begin
FDict.Free;
inherited;
end;
function TSet<T>.Contains(const Value: T): Boolean;
begin
Result := FDict.ContainsKey(Value);
end;
procedure TSet<T>.Include(const Value: T);
begin
FDict.AddOrSetValue(Value, 0);
end;
procedure TSet<T>.Exclude(const Value: T);
begin
FDict.Remove(Value);
end;
Run Code Online (Sandbox Code Playgroud)
我没有编译这段代码,所以你可能需要修复我犯的任何错误.您可能希望将其扩展为更强大.但希望这可以告诉你如何开始.
| 归档时间: |
|
| 查看次数: |
1593 次 |
| 最近记录: |