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的更多相关文章

  1. Delphi 2010 中的泛型

    Delphi 2010 中的泛型 2010已发布很长时间了,口碑还不错,准备用它开发下一项目,但对泛型等新东西的认识还不够,就搜了一下,发现下面这篇文章,还不错,大家一起补补课吧! C++中的模板.C ...

  2. TStringList 与 泛型字典TDictionary 的 哈希功能效率PK

    结论: 做HashMap 映射 功能的时候 ,字典TDictionary 功能更强大,且效率更高,比如不仅仅可以存String,还可以存结构和类. TDictionary类是一个name,value容 ...

  3. Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>

    Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持. Generics.Collections 包含 ...

  4. delphi 泛型 c++builder 泛型

    delphi 泛型 System.Generics.Collections.pas TList<T> http://docwiki.embarcadero.com/Libraries/Be ...

  5. 泛型 for to/in 遍历 PK 效率;TEnumerator、TEnumerable

    再使用泛型的时候,经常需要用到遍历功能: 只要继承了 TEnumerator 或 TEnumerable 这两个抽象类的 都具有遍历功能. 当然没有继承这两个抽象类的 也具有使用 for in 来遍历 ...

  6. Dephi泛型

    TArray TEnumerator(抽象) TEnumerable(抽象) 实际使用:TList TQueue TStack TPair TDictionary ,内部都包含 TValueEnume ...

  7. Dephi泛型generic的应用

    Dephi泛型generic的应用   泛型在C++, C#中已有广泛应用,Delphi自2009版本也引入泛型,典型的应用如TList,TDictionary.如果你熟悉C#,其用法十分类似. 比如 ...

  8. Delphi 新语法:泛型

      这里的新语法一般指Delphi7不支持的语法,高版本中会经常遇到,所以花一点时间学会使用它.泛型是一种特殊的类型.你不用一开始就指明参数的具体类型,而是先定义一个类型变量,在使用的时候再确定参数的 ...

  9. 一起学 Java(三) 集合框架、数据结构、泛型

    一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...

  10. .NET面试题系列[8] - 泛型

    “可变性是以一种类型安全的方式,将一个对象作为另一个对象来使用.“ - Jon Skeet .NET面试题系列目录 .NET面试题系列[1] - .NET框架基础知识(1) .NET面试题系列[2] ...

随机推荐

  1. C# WinForm 检查目标主机的端口是否可连接

    一个小工具. namespace IPPort_CheckTool { partial class MainForm { /// <summary> /// 必需的设计器变量. /// & ...

  2. Java并发容器详解,及使用场景

    并发容器的由来 在Java并发编程中,经常听到Java集合类,同步容器.并发容器,那么他们有哪些具体分类,以及各自之间的区别和优劣呢? 只有把这些梳理清楚了,你才能真正掌握在高并发的环境下,正确使用好 ...

  3. Shiftdel walkthrough Intermediate

    点击查看代码 nmap -p- -A 192.168.167.174 Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-11-12 00:09 UT ...

  4. Svelte 最新中文文档翻译(4)—— 符文(Runes)下

    前言 Svelte,一个非常"有趣".用起来"很爽"的前端框架.从 Svelte 诞生之初,就备受开发者的喜爱,根据统计,从 2019 年到 2024 年,连续 ...

  5. TensorFlow2入门与实践--CNN

    卷积神经网络CNN CNN原理 关于CNN的原理本文使用代码的方式来直观理解卷积中各个操作过程. 卷积 卷积层是CNN的核心组件,通过可学习的卷积核在输入特征图上进行滑动窗口操作.每个位置上,卷积核与 ...

  6. WitAwards 2024荣耀登榜!AOne载誉而归!

    近日,FCIS 2024网络安全创新大会在上海举办.本次大会以"迈向安全服务化时代"为主题,邀请来自全球的网安精英.技术专家.CISO/CSO.白帽子.创业者等展开深度对话,分享与 ...

  7. 浅谈基于SASE的安全云服务

    本文分享自天翼云开发者社区<浅谈基于SASE的安全云服务>,作者:姚****亮 SASE(secure access service edge安全访问服务边缘):是一种安全框架,结合了软件 ...

  8. windows 安装Nacos步骤

    一.Nacos中文文档网址 1.Nacos官网地址:https://nacos.io/en-us/ Nacos中文文档网址:Nacos 快速开始https://nacos.io/zh-cn/docs/ ...

  9. Prometheus修改数据存储位置

    Prometheus修改数据存储位置 Prometheus的数据存储位置可以通过配置文件中的 --storage.tsdb.path 参数来指定.默认情况下,数据存储在Prometheus安装目录下的 ...

  10. IDEA debug时候直接报ClassNotFoundException,代码正常,也可以正常运行

    原因,是因为在某些类误点了断点,需要取消