Dictionary Data Structure
- Dictionary is one of the most used Data Structures for storing data in the key-value format.
- Each element in a dictionary data structure is required to have a key, and that key must be paired with a value.
- To put it another way, the Dictionary data structure is used to store information in key-value pairs.
- The Dictionary data structure is sometimes known as an associative array, a map, or a symbol table, but it is most commonly referred to as Dictionary.
- Many prominent languages contain Dictionary or associative array as a primitive data type, whereas languages that do not include Dictionary or associative array as a primitive data type include Dictionary or associative array in their software libraries.
- Content-addressable memory is a direct kind of hardware-level support for the Dictionary or associative array.
On a Dictionary or associative array, the following operations are performed:
Add or Insert:
- A new pair of keys and values is added to the Dictionary or associative array object when you use the Add or Insert operation.
Replace or reassign:
- The Replace or reassign procedure modifies or replaces an existing value that is connected with a key.
- To put it another way, a new value is mapped to an existing key.
Delete or remove:
- When you delete or remove an element from a Dictionary or associative array object, the existing element is unmapped.
Find or Lookup:
- The value associated with a key is searched by giving the key as a search argument in the Find or Lookup operation.
Now let us write a C++ code that will give us an idea about how to use Dictionary or associative array and their basic functionalities in C++.
Code:
Output:
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 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 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