Delphi 有没有一种简单的方法可以将TDictionary内容复制到另一个目录中?
有没有一种方法或简单的方法可以将一个词典内容复制到另一个词典内容中? 假设我有以下声明Delphi 有没有一种简单的方法可以将TDictionary内容复制到另一个目录中?,delphi,generics,delphi-xe2,deep-copy,tdictionary,Delphi,Generics,Delphi Xe2,Deep Copy,Tdictionary,有没有一种方法或简单的方法可以将一个词典内容复制到另一个词典内容中? 假设我有以下声明 type TItemKey = record ItemID: Integer; ItemType: Integer; end; TItemData = record Name: string; Surname: string; end; TItems = TDictionary<TItemKey, TItemData>; var // th
type
TItemKey = record
ItemID: Integer;
ItemType: Integer;
end;
TItemData = record
Name: string;
Surname: string;
end;
TItems = TDictionary<TItemKey, TItemData>;
var
// the Source and Target have the same types
Source, Target: TItems;
begin
// I can't find the way how to copy source to target
end;
类型
TItemKey=记录
ItemID:整数;
ItemType:整数;
结束;
滴度数据=记录
名称:字符串;
姓氏:弦;
结束;
滴度=t词典;
变量
//源和目标具有相同的类型
来源、目标:滴度;
开始
//我找不到将源文件复制到目标文件的方法
结束;
我想把源代码1:1复制到目标代码。有这样的方法吗
谢谢 我认为这应该可以做到:
var
LSource, LTarget: TItems;
LKey: TItemKey;
begin
LSource := TItems.Create;
LTarget := TItems.Create;
try
for LKey in LSource.Keys do
LTarget.Add(LKey, LSource.Items[ LKey ]);
finally
LSource.Free;
LTarget.Free;
end; // tryf
end;
TDictionary有一个构造函数,允许您传入另一个集合对象,该对象将通过复制原始集合的内容来创建新的集合对象。这就是你要找的吗
constructor Create(Collection: TEnumerable<TPair<TKey,TValue>>); overload;
目标将被创建为源代码的副本(或至少包含源代码中的所有项)。如果您想更进一步,这里有另一种方法:
type
TDictionaryHelpers<TKey, TValue> = class
public
class procedure CopyDictionary(ASource, ATarget: TDictionary<TKey,TValue>);
end;
...implementation...
{ TDictionaryHelpers<TKey, TValue> }
class procedure TDictionaryHelpers<TKey, TValue>.CopyDictionary(ASource,
ATarget: TDictionary<TKey, TValue>);
var
LKey: TKey;
begin
for LKey in ASource.Keys do
ATarget.Add(LKey, ASource.Items[ LKey ] );
end;
unit uExample;
interface
uses
System.Generics.Collections;
type
TStringStringDictionary = class(TDictionary<string,string>)
public
procedure Assign(const aSSD: TStringStringDictionary);
end;
implementation
procedure TStringStringDictionary.Assign(const aSSD: TStringStringDictionary );
var
lKey: string;
begin
Clear;
for lKey in aSSD.Keys do
Add(lKey, aSSD.Items[lKey]); // Or use copy constructors for objects to be duplicated
end;
end.
类型
TDictionaryHelpers=类
公众的
类过程CopyDictionary(ASource,atatarget:TDictionary);
结束;
实施
{TDictionaryHelpers}
类过程TDictionaryHelpers.CopyDictionary(ASource,
a目标:t词典);
变量
LKey:TKey;
开始
对于资源中的LKey。钥匙可以
添加(LKey,ASource.Items[LKey]);
结束;
根据您对键和值的定义使用:
TDictionaryHelpers<TItemKey, TItemData>.CopyDictionary(LSource, LTarget);
TDictionaryHelpers.CopyDictionary(LSource,LTarget);
在指定时构造新实例可能会产生副作用,例如,其他地方的对象引用无效。泛型方法不能深度复制引用的类型
我将采用一种更简单的方法:
type
TDictionaryHelpers<TKey, TValue> = class
public
class procedure CopyDictionary(ASource, ATarget: TDictionary<TKey,TValue>);
end;
...implementation...
{ TDictionaryHelpers<TKey, TValue> }
class procedure TDictionaryHelpers<TKey, TValue>.CopyDictionary(ASource,
ATarget: TDictionary<TKey, TValue>);
var
LKey: TKey;
begin
for LKey in ASource.Keys do
ATarget.Add(LKey, ASource.Items[ LKey ] );
end;
unit uExample;
interface
uses
System.Generics.Collections;
type
TStringStringDictionary = class(TDictionary<string,string>)
public
procedure Assign(const aSSD: TStringStringDictionary);
end;
implementation
procedure TStringStringDictionary.Assign(const aSSD: TStringStringDictionary );
var
lKey: string;
begin
Clear;
for lKey in aSSD.Keys do
Add(lKey, aSSD.Items[lKey]); // Or use copy constructors for objects to be duplicated
end;
end.
单位举例;
接口
使用
系统、泛型、集合;
类型
TStringStringDictionary=类(TDictionary)
公众的
程序分配(const aSSD:TStringStringDictionary);
结束;
实施
过程TStringStringDictionary.Assign(const aSSD:TStringStringDictionary);
变量
lKey:字符串;
开始
清楚的
给艾尔基,钥匙可以
添加(lKey,关联项[lKey]);//或者对要复制的对象使用复制构造函数
结束;
结束。
您能解释一下为什么分配LNewKey:=LKey;而不仅仅是在表达式LTarget.Add(Lkey,LSource.Items[Lkey])中使用两次Lkey;我认为这会更快:var项:TPair。。。对于LSource中的项,执行atatarget.Add(Item.Key,Item.Value)代码>我认为这会更快:var项:TPair。。。对于ASource do atatarget.Add(Item.Key,Item.Value)中的项代码>