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] ...
随机推荐
- 10.3 - AM - 模拟赛 总结
复盘 T1 很水,一道异或求和,但是某两位仁兄因没打括号而死. T2 很水,一道字符串处理,但是我和某位仁兄因没特判而死(虽然没有 hack 掉我,所以我理论上还是满分). T3 不水,看了很久,没想 ...
- WPF test animation robot(simulate blink,mouse talk)
WPF 动画,模拟机器人眨眼,说话. using System; using System.Collections.Generic; using System.Linq; using System.T ...
- Java虚拟机调优-典型配置举例
背景: 以下配置主要针对分代垃圾回收算法而言. 堆大小设置 年轻代的设置很关键 JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的 ...
- iptables使用详解(示例如何屏蔽docker 暴露的端口)
[场景]搭建了一台CentOS虚拟机,并在上面搭了DOCKER,然后再DOCKER中安装Mysql.但只要将网络端口映射到宿主机上,那么外部网络就可以直接访问该数据.为此,我们需要使用防火墙(暂且不考 ...
- openSSL学习-0
本文记录openSSL密码库的学习. 首先推荐:openSSL中文手册 介绍 openssl是一个功能丰富且自包含的开源安全工具箱. 它提供的主要功能有:SSL协议实现(包括SSLv2.SSLv3和T ...
- 某教育网站疑似删库。。。没备份。。。数据全没了。。。Sealos 带你一分钟满血复活
2025 年 1 月 15 日,微信群里有人爆料,某教育网站疑似删库,导致网站无法访问.具体的问题是数据库被格式化了,而且也没有备份,连数据库表结构都没有,不仅业务瘫痪,也无法拉起新的应用,实在是有点 ...
- ORACLE 分页和行限制
行限制:示例 (此语法从12C版本开始支持) 以下语句返回具有最低employee_id值的 5 名员工: SELECT employee_id, last_name FROM Employees O ...
- react时时获取表单数据
import React, { Component } from "react"; export class TestHanderClick extends Component { ...
- CSP 初赛要点复习
位运算 逻辑与.按位与之类的东西是不同的!"逻辑"的是判断两个数都不为 \(0\),"按位"的是判断两个数的每一个二进制位与的结果,是不同的.其他运算也类似. ...
- MinIO笔记
MinIO (网站 https://min.io/) 是开源的对象存储项目, 用Go实现, 支持Linux环境, 客户端支Java,Python,Javacript, Go等语言. 在分布式项目中, ...