nbhh的泛型:TDictionary

type
TCity = class
Country: String;
Latitude: Double;
Longitude: Double;
end; const
EPSILON = 0.0000001; var
Dictionary: TDictionary<String, TCity>;
City, Value: TCity;
Key: String; begin
{ Create the dictionary. }
Dictionary := TDictionary<String, TCity>.Create;
City := TCity.Create;
{ Add some key-value pairs to the dictionary. }
City.Country := 'Romania';
City.Latitude := 47.16;
City.Longitude := 27.58;
Dictionary.Add('Iasi', City); City := TCity.Create;
City.Country := 'United Kingdom';
City.Latitude := 51.5;
City.Longitude := -0.17;
Dictionary.Add('London', City); City := TCity.Create;
City.Country := 'Argentina';
{ Notice the wrong coordinates }
City.Latitude := 0;
City.Longitude := 0;
Dictionary.Add('Buenos Aires', City); { Display the current number of key-value entries. }
writeln('Number of pairs in the dictionary: ' +
IntToStr(Dictionary.Count)); // Try looking up "Iasi".
if (Dictionary.TryGetValue('Iasi', City) = True) then
begin
writeln(
'Iasi is located in ' + City.Country +
' with latitude = ' + FloatToStrF(City.Latitude, ffFixed, 4, 2) +
' and longitude = ' + FloatToStrF(City.Longitude, ffFixed, 4, 2)
);
end
else
writeln('Could not find Iasi in the dictionary'); { Remove the "Iasi" key from dictionary. }
Dictionary.Remove('Iasi'); { Make sure the dictionary's capacity is set to the number of entries. }
Dictionary.TrimExcess; { Test if "Iasi" is a key in the dictionary. }
if Dictionary.ContainsKey('Iasi') then
writeln('The key "Iasi" is in the dictionary.')
else
writeln('The key "Iasi" is not in the dictionary.'); { Test how (United Kingdom, 51.5, -0.17) is a value in the dictionary but
ContainsValue returns False if passed a different instance of TCity with the
same data, as different instances have different references. }
if Dictionary.ContainsKey('London') then
begin
Dictionary.TryGetValue('London', City);
if (City.Country = 'United Kingdom') and (CompareValue(City.Latitude, 51.5, EPSILON) = EqualsValue) and (CompareValue(City.Longitude, -0.17, EPSILON) = EqualsValue) then
writeln('The value (United Kingdom, 51.5, -0.17) is in the dictionary.')
else
writeln('Error: The value (United Kingdom, 51.5, -0.17) is not in the dictionary.');
City := TCity.Create;
City.Country := 'United Kingdom';
City.Latitude := 51.5;
City.Longitude := -0.17;
if Dictionary.ContainsValue(City) then
writeln('Error: A new instance of TCity with values (United Kingdom, 51.5, -0.17) matches an existing instance in the dictionary.')
else
writeln('A new instance of TCity with values (United Kingdom, 51.5, -0.17) does not match any existing instance in the dictionary.');
City.Free;
end
else
writeln('Error: The key "London" is not in the dictionary.'); { Update the coordinates to the correct ones. }
City := TCity.Create;
City.Country := 'Argentina';
City.Latitude := -34.6;
City.Longitude := -58.45;
Dictionary.AddOrSetValue('Buenos Aires', City); { Generate the exception "Duplicates not allowed". }
try
Dictionary.Add('Buenos Aires', City);
except
on Exception do
writeln('Could not add entry. Duplicates are not allowed.');
end; { Display all countries. }
writeln('All countries:');
for Value in Dictionary.Values do
writeln(Value.Country); { Iterate through all keys in the dictionary and display their coordinates. }
writeln('All cities and their coordinates:');
for Key in Dictionary.Keys do
begin
writeln(Key + ': ' + FloatToStrF(Dictionary.Items[Key].Latitude, ffFixed, 4, 2) + ', ' +
FloatToStrF(Dictionary.Items[Key].Longitude, ffFixed, 4, 2));
end; { Clear all entries in the dictionary. }
Dictionary.Clear; { There should be no entries at this point. }
writeln('Number of key-value pairs in the dictionary after cleaning: ' + IntToStr(Dictionary.Count)); { Free the memory allocated for the dictionary. }
Dictionary.Free;
City.Free;
readln;
end.

nbhh的泛型:TDictionary的更多相关文章
- Delphi 2010 中的泛型
Delphi 2010 中的泛型 2010已发布很长时间了,口碑还不错,准备用它开发下一项目,但对泛型等新东西的认识还不够,就搜了一下,发现下面这篇文章,还不错,大家一起补补课吧! C++中的模板.C ...
- TStringList 与 泛型字典TDictionary 的 哈希功能效率PK
结论: 做HashMap 映射 功能的时候 ,字典TDictionary 功能更强大,且效率更高,比如不仅仅可以存String,还可以存结构和类. TDictionary类是一个name,value容 ...
- Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>
Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持. Generics.Collections 包含 ...
- delphi 泛型 c++builder 泛型
delphi 泛型 System.Generics.Collections.pas TList<T> http://docwiki.embarcadero.com/Libraries/Be ...
- 泛型 for to/in 遍历 PK 效率;TEnumerator、TEnumerable
再使用泛型的时候,经常需要用到遍历功能: 只要继承了 TEnumerator 或 TEnumerable 这两个抽象类的 都具有遍历功能. 当然没有继承这两个抽象类的 也具有使用 for in 来遍历 ...
- Dephi泛型
TArray TEnumerator(抽象) TEnumerable(抽象) 实际使用:TList TQueue TStack TPair TDictionary ,内部都包含 TValueEnume ...
- Dephi泛型generic的应用
Dephi泛型generic的应用 泛型在C++, C#中已有广泛应用,Delphi自2009版本也引入泛型,典型的应用如TList,TDictionary.如果你熟悉C#,其用法十分类似. 比如 ...
- Delphi 新语法:泛型
这里的新语法一般指Delphi7不支持的语法,高版本中会经常遇到,所以花一点时间学会使用它.泛型是一种特殊的类型.你不用一开始就指明参数的具体类型,而是先定义一个类型变量,在使用的时候再确定参数的 ...
- 一起学 Java(三) 集合框架、数据结构、泛型
一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...
- .NET面试题系列[8] - 泛型
“可变性是以一种类型安全的方式,将一个对象作为另一个对象来使用.“ - Jon Skeet .NET面试题系列目录 .NET面试题系列[1] - .NET框架基础知识(1) .NET面试题系列[2] ...
随机推荐
- MyBatis中的 10 个宝藏技巧!
前言 说到 MyBatis,很多小伙伴都会用,但未必用得"惊艳". 实际上,这个轻量级的持久层框架还有很多隐藏的"宝藏技巧". 如果你能掌握这些技巧,不但能让开 ...
- 揭秘10种主流PLC在ModbusTCP通信中的速度表现!
大家好!我是付工. 通透!终于把ModbusRTU弄明白了 这样看来,ModbusTCP协议太简单了 太简单了!C#轻松实现Modbus通信 前面给大家介绍了一系列关于Modbus和ModbusTCP ...
- AVX512
最近接触到SIMD编码,就不可避免的查到了AVX指令集,两者有什么关系呢,了解一下? 问:AVX是什么? 答:是一套指令集 下面具体看: AVX 以下内容主要转载自:AVX指令集是什么?它的应用又有哪 ...
- Oracle trunc的使用
在生产环境中我们经常会用到只取年月日或者时间处理的场景,大多数人用的都是to_char(string,'yyyy-mm-dd')或者to_date(string,'yyyy-mm-dd')来处理,不说 ...
- 天翼云云电脑:IAAS基础设施带来的计算革新
本文分享自天翼云开发者社区<天翼云云电脑:IAAS基础设施带来的计算革新>,作者:不知不觉 在当今这个数字化快速发展的时代,云计算作为一种新兴的信息技术,已经逐渐成为企业和个人日常运营的重 ...
- 手把手教你在个人电脑部署本地知识库(基于RAGFlow + DeepSeek [+ Ollama])
1. 实现方案及准备工作 按照教程一步一步操作,基本没有什么太大难度,稍显麻烦的可能就是因网络问题有些资源无法下载,对于镜像无法下载的问题,文中也提供了替代的方法,但是github访问不稳定这点 ...
- WinForm 进度条显示进度百分比
参考: https://blog.csdn.net/zhuimengshizhe87/article/details/20640157 WinForm中显示进度条百分比有多种方式: 1. 添加 Lab ...
- CF145C Lucky Subsequence 题解
首先,我们对这个幸运数进行分析,发现: \(10^9\) 以内只有 \(1023\) 个幸运数,即 \(\sum\limits_{i=0}^92^i\) 个. 考虑对幸运数和非幸运数分类讨论. 幸运数 ...
- 无线路由器dBi越大越好吗?
无线路由器dBi越大越好吗? 目前,常见的无线路由器,通过查看参数可知,大多为3dBi.5dBi或7dBi,对于用户来说,这个数值到底是越大越好,还是越小越好呢?对于这个问题,其实通过下面这张天线增益 ...
- 【COM3D2Mod 制作教程(1)】教程简介
[COM3D2Mod 制作教程(1)]教程简介 教程来自:https://bdffzi.github.io/opencom3d2/#/Research/MakeMod(含配套视频) 前言 没有Mod ...