gpt4 book ai didi

delphi - 在创建示例代码期间填充 TDictionary

转载 作者:行者123 更新时间:2023-12-03 15:46:41 25 4
gpt4 key购买 nike

有人有TDictionary<TKey, TValue>的示例代码吗?在其构造函数期间填充?

最佳答案

显然你只是想要一句台词,所以我尝试了一下,实现了 TDictHelper允许使用单行代码创建和填充字典。

<小时/>

使用任何形式的单行初始化字典的问题是它需要一对值,而我们没有所需的良好语法来传递这些对。例如,如果需要使用 TPair<Key, Value>.Create(A, B)添加到字典中的每一对值的语法,这将是一个丑陋的衬里。

我确实想出了一些好看的替代方案;第一个的用法如下:

  with TDictHelper<Integer, string> do
Dict := Make([P(1, 'one'), P(2, 'two')]);

with的使用是必需的,因为 TDictHelper我实现的类有一个 Make例程采用 TPair<Key, Value> 数组作为参数;如果我把它写成:

Dict := TDictHelper<Integer, string>.Make(TPair<Integer, string>.Create(1, 'one'), TPair<Integer, string>.Create(2, 'two'));

它会起作用,但会非常非常难看!

自从使用with可能会出现问题(特别是如果您想使用两种词典),我提供了另一种语法;不幸的是,这个无法扩展,它很快就会变得非常丑陋:

  Dict := TDictHelper<Integer, string>.Make([1, 2], ['one', 'two']);

此替代方案采用两个单独的键和值数组,将它们组合在 Make 中方法。对于 2-3 个元素来说看起来没问题,但无法扩展:如果您有 10 个元素并且需要删除第 7 对元素怎么办?您需要计算元素,这很容易出错。

这是完整的代码,不多说:

program Project25;

{$APPTYPE CONSOLE}

uses
SysUtils, Generics.Collections;

type
TDictHelper<Key, Value> = class
public
class function P(const K:Key; const V:Value): TPair<Key, Value>;
class function Make(init: array of TPair<Key, Value>): TDictionary<Key, Value>;overload;
class function Make(KeyArray: array of Key; ValueArray: array of Value): TDictionary<Key, Value>;overload;
end;

{ TDictHelper<Key, Value> }

class function TDictHelper<Key, Value>.Make(init: array of TPair<Key, Value>): TDictionary<Key, Value>;
var P: TPair<Key, Value>;
begin
Result := TDictionary<Key, Value>.Create;
for P in init do
Result.AddOrSetValue(P.Key, P.Value);
end;

class function TDictHelper<Key, Value>.Make(KeyArray: array of Key;
ValueArray: array of Value): TDictionary<Key, Value>;
var i:Integer;
begin
if Length(KeyArray) <> Length(ValueArray) then
raise Exception.Create('Number of keys does not match number of values.');
Result := TDictionary<Key, Value>.Create;
for i:=0 to High(KeyArray) do
Result.AddOrSetValue(KeyArray[i], ValueArray[i]);
end;

class function TDictHelper<Key, Value>.P(const K: Key;
const V: Value): TPair<Key, Value>;
begin
Result := TPair<Key, Value>.Create(K, V);
end;

// ============================== TEST CODE FOLLOWS

var Dict: TDictionary<Integer, string>;
Pair: TPair<Integer, string>;

begin
try
try
// Nice-looking but requires "with" and you can't work with two kinds of DictHelper at once
with TDictHelper<Integer, string> do
Dict := Make([P(1, 'one'), P(2, 'two')]);
// Use the array
for Pair in Dict do
WriteLn(Pair.Key, ' = ', Pair.Value);
Dict.Free;

// Passing the Keys and the Values in separate arrays; Works without "with" but it would
// be difficult to maintain for larger number of key/value pairs
Dict := TDictHelper<Integer, string>.Make([1, 2], ['one', 'two']);
// Use the array
for Pair in Dict do
WriteLn(Pair.Key, ' = ', Pair.Value);
Dict.Free;

except on E:Exception do
WriteLn(E.ClassName, #13#10, E.Message);
end;
finally ReadLn;
end;
end.

关于delphi - 在创建示例代码期间填充 TDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14490275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com