Associative Arrays
Explore how associative arrays work in D programming, enabling key-value mapping with fast access using hash tables. Understand syntax, operations like adding or removing pairs, and useful properties to manage data effectively.
Associative arrays
Associative array is a feature that is found in most modern high-level languages. They are very fast data structures that work like mini databases and are used in many programs.
We have seen in the arrays lesson that plain arrays are containers that store their elements side-by-side and provide access to them by index. An array that stores the names of the days of the week can be defined like this:
string[] dayNames =
[ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ];
Note:
stringdatatype will be covered later in this chapter.
The name of a specific day can be accessed by its index in that array:
writeln(dayNames[1]); // prints "Tuesday"
How are associative arrays different from plain arrays?
-
The fact that plain arrays provide access to their values through index numbers can be described as an association of indexes with values. In other words, arrays map indexes to values. Plain arrays can use only integers as indexes.
Associative arrays allow indexing not only using integers but also using any other type. They map the values of one type to the values of another type. The values of the type that associative arrays map from are called ...