gpt4 book ai didi

delphi - 有没有一种简单的方法将 TDictionary 内容复制到另一个中?

转载 作者:行者123 更新时间:2023-12-03 14:36:55 28 4
gpt4 key购买 nike

是否有一种方法或简单的方法可以将一个 TDictionary 内容复制到另一个 TDictionary 内容中?假设我有以下声明

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;

我想将源 1:1 复制到目标。有这样的方法吗?

谢谢!

最佳答案

TDictionary 有一个构造函数,允许您传入另一个集合对象,该对象将通过复制原始集合对象的内容来创建新集合对象。这是您要找的吗?

constructor Create(Collection: TEnumerable<TPair<TKey,TValue>>); overload;

所以你会使用

Target := TItems.Create(Source);

目标将被创建为源的副本(或至少包含源中的所有项目)。

关于delphi - 有没有一种简单的方法将 TDictionary 内容复制到另一个中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9787296/

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