如何使用TEnumerator按键排序顺序浏览我的TDictionary?
我有这样的事情:
var
Dic: TDictionary<string, string>;
Enum: TPair<string, string>;
begin
Dic := TDictionary<string, string>.create;
Dic.Add('Tired', 'I have been working on this too long');
Dic.Add('Early', 'It is too early in the morning to be working on this');
Dic.Add('HelpMe', 'I need some help');
Dic.Add('Dumb', 'Yes I know this example is dumb');
{ I want to do the following but do it in sorted order by Enum.Key }
for Enum in Dic do
some processing with Enum.Key and Enum.Value;
Dic.Free;
end;
Run Code Online (Sandbox Code Playgroud)
所以我想按顺序处理我的字典:Dumb,Early,HelpMe,Tired.
不幸的是,Delphi帮助在描述一般的枚举器和TEnumerator如何具体工作方面是非常小的,并没有给出我能找到的例子.关于在Delphi中使用带有泛型的枚举器,网上也很少写. …
我有一个函数,我存储一些键值对,当我迭代它们时,我得到两次错误:[dcc32错误] App.pas(137):E2149类没有默认属性.这是我的代码的一部分:
function BuildString: string;
var
i: Integer;
requestContent: TDictionary<string, string>;
request: TStringBuilder;
begin
requestContent := TDictionary<string, string>.Create();
try
// add some key-value pairs
request := TStringBuilder.Create;
try
for i := 0 to requestContent.Count - 1 do
begin
// here I get the errors
request.Append(requestContent.Keys[i] + '=' +
TIdURI.URLEncode(requestContent.Values[i]) + '&');
end;
Result := request.ToString;
Result := Result.Substring(0, Result.Length - 1); //remove the last '&'
finally
request.Free;
end;
finally
requestContent.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)
我需要收集字典中每个项目的信息.我该如何解决?
有一种方法或简单方法如何将一个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;
Run Code Online (Sandbox Code Playgroud)
我想将源1:1复制到目标.有这样的方法吗?
谢谢!
你能在TDictionary中使用记录作为Key值吗?我想基于字符串,整数和整数的组合来查找对象.
TUserParKey=record
App:string;
ID:integer;
Nr:integer;
end;
Run Code Online (Sandbox Code Playgroud)
...
var
tmpKey:TUserParKey;
tmpObject:TObject;
begin
tmpObject:= TTObject.Create(1);
tmpKey.App:='1';
tmpKey.ID :=1;
tmpKey.Nr :=1;
DTUserPars.Add(tmpKey,tmpObject)
Run Code Online (Sandbox Code Playgroud)
...
var
tmpKey:TUserParKey;
begin
tmpKey.App:='1';
tmpKey.ID :=1;
tmpKey.Nr :=1;
if not DTUserPars.TryGetValue(tmpKey,Result) then begin
result := TTObject.Create(2);
end;
Run Code Online (Sandbox Code Playgroud)
这返回对象2.
我已经定义了一个派生自TDictionary的集合,需要定义一个应用额外过滤器的自定义枚举器.
我被卡住,因为我无法访问TDictionary FItems数组(它是私有的)所以我无法定义MoveNext方法
您将如何继续重新定义从TDictionary派生的类的过滤枚举器?
这是一个简单的代码来说明我想要做的事情:
TMyItem = class(TObject)
public
IsHidden:Boolean; // The enumerator should not return hidden items
end;
TMyCollection<T:TMyItem> = class(TDictionary<integer,T>)
public
function GetEnumerator:TMyEnumerator<T>; // A value filtered enumerator
type
TMyEnumerator = class(TEnumerator<T>)
private
FDictionary: TMyCollection<integer,T>;
FIndex: Integer;
function GetCurrent: T;
protected
function DoGetCurrent: T; override;
function DoMoveNext: Boolean; override;
public
constructor Create(ADictionary: TMyCollection<integer,T>);
property Current: T read GetCurrent;
function MoveNext: Boolean;
end;
end;
function TMyCollection<T>.TMyEnumerator.MoveNext: Boolean;
begin
// In below code, FIndex is not accessible, so I can't …Run Code Online (Sandbox Code Playgroud) 如何在Delphi 2009中按升序键的整数键对TDictionary进行排序?
我有一个TDictionary<TKeyClass, TValueClass>.
我想完成类似的事情
for i := 0 to MyDictionary.Count -1 do
ShowMessage(MyDictionary.Keys[i].AStringProperty)
Run Code Online (Sandbox Code Playgroud)
我不能再访问密钥如果我完全了解它我就可以使用它们.
是唯一的替代品TDictionary<TValueClass, TKeyValue>吗?所以我可以循环使用Keys?
我找到的工作方法是创建一个,TList<TKeyClass>但这是我不喜欢的东西.
我尝试使用标准delphi序列化程序序列化/反序列化标准delphi容器.
procedure TForm7.TestButtonClick(Sender: TObject);
var
dict: TDictionary<Integer, Integer>;
jsonValue: TJSONValue;
begin
//serialization
dict := TDictionary<Integer, Integer>.Create;
dict.Add(1, 1);
jsonValue := TJsonConverter.ObjectToJSON(dict);
dict.Free;
//deserialization
dict := TJsonConverter.JSONToObject(jsonValue) as TDictionary<Integer, Integer>;
try
Assert(dict.ContainsKey(1), 'deserialization error - key not found');
except
Assert(false, 'deserialization error - dict object broken');
end;
end;
Run Code Online (Sandbox Code Playgroud)
有一种方法我将对象转换为JSON,反之亦然;
class function TJsonConverter.JSONToObject(AJSONValue: TJSONValue): TObject;
var
lUnMarshal: TJSONUnMarshal;
begin
lUnMarshal := TJSONUnMarshal.Create();
try
Result := lUnMarshal.Unmarshal(AJSONValue);
finally
lUnMarshal.Free;
end;
end;
class function TJsonConverter.ObjectToJSON(AData: TObject): TJSONValue;
var
lMarshal: TJSONMarshal;
begin
lMarshal := TJSONMarshal.Create(); …Run Code Online (Sandbox Code Playgroud) 有一个类TPerson.众所周知,FSecondName对每个对象都是唯一的.
type
TPerson = class(TObject)
private
FAge: Integer;
FFirstName: String;
FSecondName: String;
public
property Age: Integer read FAge;
property FirstName: String read FFirstName;
property SecondName: String read FSecondName;
constructor Create;
end;
Run Code Online (Sandbox Code Playgroud)
如何添加类字段(如C#中的静态字段)Persons:TDictionary(String,TPerson),其中键是SecondName,值是类TPerson的对象.
谢谢!
对于Delphi项目(使用RAD Studio XE7构建),我想创建一个画笔字典.每个字典项包含一个TMyBrush对象作为键,描述要检索的画笔,以及GDI +画笔作为值.
TMyBrush类包含3个字段
在我的字典中,我想根据他的特征检索画笔,而不是他的实例.例如,我希望通过创建本地TMyBrush实例,将其配置为黑色实体,并使用TryGetValue()函数获取匹配的GDI +值,从我的字典中获取黑色实心画笔.为此,我创建了一个TMyBrushComparer.
编写Equals()函数对我来说不是问题.但是我不知道编写GetHashCode()函数的最佳做法是什么.我倾向于写一个这样的函数:
function TMyBrushComparer.GetHashCode(const pValue: TMyBrush): Integer;
begin
Result := BobJenkinsHash(pValue, SizeOf(TMyBrush), 0);
end;
Run Code Online (Sandbox Code Playgroud)
但是我觉得这不是一个很好的做法,这是正确的吗?那么,为我的TMyBrushComparer编写一个好的GetHashCode()函数的最佳实践是什么?
问候
delphi ×10
tdictionary ×10
generics ×3
collections ×1
deep-copy ×1
delphi-2009 ×1
delphi-xe2 ×1
enumerator ×1
gethashcode ×1
hash ×1
iteration ×1
json ×1
sorting ×1
static ×1