Map
in Java or std::unordered_map
in C ++. // . .: value — , key — value[key] myAssociativeArray;
[]
. import std.stdio; void main() { int[int] squares; // for (int i = 0; i <= 10; ++i) squares[i] = i * i; // writeln(squares); }
[0:0, 6:36, 7:49, 2:4, 3:9, 10:100, 1:1, 8:64, 5:25, 4:16, 9:81]
core.exception.RangeError
not exist will result in a core.exception.RangeError
error.remove()
function. aa.remove("hello");
in
operator, which returns a pointer to the value. If the key does not exist, the pointer will be null
. int[int] squares; // ... int* p = 10 in squares; // . .: null , is if (p !is null) writeln(*p); else writeln(" .");
foreach (key; aa.keys) aa.remove(key);
null
value. aa = null; // aa[1] = 1; //
remove()
or keys
. Below are the rest:Property | Description |
---|---|
.sizeof | Returns the size of the reference to the associative array. In 32-bit builds, this is 4, and in 64-bit builds 8. |
.length | Returns the number of values in an array. Unlike dynamic arrays, this property is read only. |
.dup | Creates an associative array of the same size and copies the contents of the first array into it. |
.keys | Returns a dynamic array whose elements are the keys of the source associative array. |
.values | Returns a dynamic array whose elements are the values of the source associative array. |
.rehash | On-site reorganizes the array, optimizing the search for it. rehash is useful, for example, when a symbol table is loaded into a program, and then it is necessary to search by it. Returns a reference to the reorganized array. |
.byKey() | Returns the range suitable for iteration using the foreach keys of an associative array. |
.byValue() | Returns a range suitable for iteration using the foreach of an associative array. |
.byKeyValue() | Returns the range suitable for iteration using the foreach key-value pairs of an associative array. The returned pairs are represented as an opaque type with the properties .key and .value , which allow accessing the key and the value of the pair, respectively. |
.get(Key key, lazy Value defVal) | Looking for a key . If it exists, returns the corresponding value; if it does not exist, executes and returns the default defVal . |
Source: https://habr.com/ru/post/274723/
All Articles