字典数据结构

2025 年 4 月 19 日 | 13 分钟阅读

字典是一种重要的数据结构,通常用于以键值对格式存储数据。字典数据结构中存在的每个元素都必须有一个键,并且该特定键都关联着某个值。换句话说,我们也可以说字典数据结构用于以键值对存储数据。字典数据结构的其他名称包括关联数组、映射、符号表,但广义上它被称为字典。

字典或关联数组是一种通用 数据结构,用于存储一组对象。

许多流行语言将字典或关联数组作为其语言中的原始数据类型,而其他不将字典或关联数组视为原始数据类型的语言已将其包含在其软件库中。对字典或关联数组的硬件级支持的直接形式是 **内容可寻址存储器**。

在字典或关联数组中,键和值之间的关系或关联称为映射。我们可以说字典中的每个值都映射到字典中存在的特定键,反之亦然。

字典或关联数组上执行的各种操作是

  • **添加或插入:** 在添加或插入操作中,一个新的键值对被添加到字典或关联数组对象中。
  • **替换或重新分配:** 在替换或重新分配操作中,与某个键关联的现有值被更改或修改。换句话说,一个新值被映射到一个已经存在的键。
  • **删除或移除:** 在删除或移除操作中,已存在的元素从字典或关联数组对象中取消映射。
  • **查找或检索:** 在查找或检索操作中,通过将键作为搜索参数传入来搜索与键关联的值。

现在,让我们看看字典数据结构在不同编程语言中的用法。

Python

一个执行所有基本四种操作(如创建、更新、删除和更新)的 Python 示例代码。

代码

输出

Enter the jersey number to be entered
45
Enter the player name to be entered
Rohit Sharma
{45: 'Rohit Sharma'}
Enter the jersey number to be entered
18
Enter the player name to be entered
Virat Kholi
Enter the jersey number to be entered
7
Enter the player name to be entered
Mahendra Singh Dhoni
Enter the jersey number to be entered
42
Enter the player name to be entered
Shikar Dhawan
{45: 'Rohit Sharma', 18: 'Virat Kholi', 7: 'Mahendra Singh Dhoni', 42: 'Shikar Dhawan'}

Enter the key whose value you want to update
42
Enter the new value that you want to assign to the key entered
Shikhar Dhawan
The dictionary after updating the values is:
{45: 'Rohit Sharma', 18: 'Virat Kholi', 7: 'Mahendra Singh Dhoni', 42: 'Shikhar Dhawan'}

Enter the key that you want to delete or remove from the dictionary object
18
The dictionary after deleting the values is:
{45: 'Rohit Sharma', 7: 'Mahendra Singh Dhoni', 42: 'Shikhar Dhawan'}

在此代码中,我们创建了一个名为 `players` 的字典对象,其中键为整数,值为

字符串。我们还创建了函数来实现字典的所有基本四种功能,即创建、删除、更新等。

正如我们在上述代码的输出中看到的,我们的字典对象有 4 个输入元素,然后我们更新了字典中的一个值,然后我们删除了字典中的一个值,最后我们打印了最终的字典对象。

Java

一个执行所有基本操作(如创建、删除和更新)的 Java 示例代码。

代码

输出

Enter the key for the Dictionary Object
name
Enter the value for the Dictionary Object
Nirnay Khajuria
Enter the key for the Dictionary Object
age
Enter the value for the Dictionary Object
23
{age=23, name=Nirnay Khajuria}


Enter the key of the element that you want to remove or delete 
age
Dictionary after removing the element(s)
{name=Nirnay Khajuria}


Enter the key of the element that you want to search or find 
name
The value of the key name is Nirnay Khajuria

在此代码中,我们创建了一个名为 **dictObject** 的字典对象,其中键为字符串,值为

字符串。我们还创建了函数来实现字典的所有基本四种功能,即创建、删除、更新、搜索等。

正如我们在上述代码的输出中看到的,我们的字典对象有两个输入元素,然后我们更新了字典中的一个值,然后我们搜索了字典中的一个值,最后我们打印了我们创建的最终字典对象。

C++

现在让我们编写一个 C++ 代码,它将让我们了解如何在 C++ 中使用字典或关联数组及其基本功能。

代码

输出

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:1

Enter the name of the country : India
Enter the capital of India : Delhi

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:1

Enter the name of the country : Dominica
Enter the capital of Dominica : Roseau

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:1

Enter the name of the country : Haiti
Enter the capital of Haiti : Port-au-prince

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:1

Enter the name of the country : USA
Enter the capital of USA : Washington

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:2

Contents of the Dictionary are : 
Name of the country Dominica: Name of the capital Roseau
Name of the country Haiti: Name of the capital Port-au-prince
Name of the country India: Name of the capital Delhi
Name of the country USA: Name of the capital Washington

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:3


Enter the name of the country that you want to delete : Haiti
Element deleted successfully.

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:2

Contents of the Dictionary are : 
Name of the country Dominica: Name of the capital Roseau
Name of the country India: Name of the capital Delhi
Name of the country USA: Name of the capital Washington

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:4

Result of the search in the dictionary is  : 
Enter the name of the country that you want to search : USA
Capital of USA is Washington

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:5


Enter the name of the country whose capital you want to update : India
Enter the name of new capital : New-Delhi       
Contents of the Dictionary updated sucessfully.

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:2

Contents of the Dictionary are : 
Name of the country Dominica: Name of the capital Roseau
Name of the country India: Name of the capital New-Delhi
Name of the country USA: Name of the capital Washington

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:0

正如我们在上面的代码中看到的,我们成功地实现了所有基本操作,包括搜索、更新、插入、删除和打印我们创建的名为 capitals 的字典对象,用于存储国家名称及其各自的首都。

为上述所有操作创建了不同的函数。首先,我们创建了一个字典并向该创建的对象添加了元素。数据插入对象完成后,我们显示了添加的数据。显示后,我们从字典对象中删除了已存在的数据。删除后,对存储国家名称及其各自首都的字典对象执行了搜索和更新操作,分别从字典对象中搜索和更新了各种元素。

因此,本文解释了字典数据结构以及我们可以在字典数据结构对象上执行的基本函数或操作。我们还了解了字典数据结构在各种编程语言(如 Java、Python 和 C++)中的用法,以及执行此数据结构上基本操作所需的功能。除了这些示例,还有各种场景可以使用字典数据结构。使用字典数据结构最理想的场景是我们需要以键值对存储数据。