delphi – 对TDictionary进行排序
发布时间:2020-12-15 09:25:03 所属栏目:大数据 来源:网络整理
导读:我对Generic Collections没有经验.我需要对TDictionary进行排序. type TSearchResult = TPairInteger,double;var target_results : TDictionaryInteger,double; session_search_results : array[0..max_searches] of TArrayTSearchResult; 我正在使用此代码
我对Generic Collections没有经验.我需要对TDictionary进行排序.
type TSearchResult = TPair<Integer,double>; var target_results : TDictionary<Integer,double>; session_search_results : array[0..max_searches] of TArray<TSearchResult>; 我正在使用此代码进行排序 session_search_results[session_search_id]:= target_results.ToArray; TArray.Sort<TSearchResult>(session_search_results[session_search_id],TComparer<TSearchResult>.Construct( function(const L,R: TSearchResult): Integer begin Result := Round(R.Value - L.Value); end )); 为什么我会收到访问冲突?我究竟做错了什么? 补充: 如果我用数组遍历数组 for i:= 0 to Length(session_search_results[session_search_id])-1 do MyDebug(IntToStr(session_search_results[session_search_id][i].Key)+' = value = ' + FloatToStr(session_search_results[session_search_id][i].Value)); 我得到一个输出: Debug Output: ==>CoreSearchText: array length=8<== Process TestApp.exe (2536) Debug Output: ==>100007 = value = 19,515<== Process TestApp.exe (2536) Debug Output: ==>100003 = value = 2,4<== Process TestApp.exe (2536) Debug Output: ==>100005 = value = 12<== Process TestApp.exe (2536) Debug Output: ==>100008 = value = 2,4<== Process TestApp.exe (2536) Debug Output: ==>100002 = value = 2,4<== Process TestApp.exe (2536) Debug Output: ==>100004 = value = 2,4<== Process TestApp.exe (2536) Debug Output: ==>100009 = value = 40,515<== Process TestApp.exe (2536) Debug Output: ==>100001 = value = 15<== Process TestApp.exe (2536) 应用排序时,访问冲突会使应用程序崩溃.阵列似乎没问题.可能是什么原因?谢谢! 解决方法
这似乎是XE中的codegen错误(也存在于XE2中),其中重新声明的通用记录和优化已打开.
该程序重现了该错误: program Project1; {$APPTYPE CONSOLE} {$O+} uses Generics.Collections,Generics.Defaults,SysUtils; type TSearchResult = TPair<Integer,Integer>; function Compare(const L,R: TSearchResult): Integer; begin Result := R.Value - L.Value; end; var values: TArray<TSearchResult>; begin try SetLength(values,3); TArray.Sort<TSearchResult>(values,TComparer<TSearchResult>.Construct(Compare)); except on E: Exception do Writeln(E.ClassName,': ',E.Message); end; Readln; end. 我把它报告为QC #106391. 一种可能的解决方案是将{$O-}添加到包含对TArray< T> .Sort的调用的单元中. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |