📜  D编程-关联数组

📅  最后修改于: 2020-11-04 05:11:28             🧑  作者: Mango


关联数组的索引不一定是整数,并且可以稀疏地填充。关联数组的索引称为Key ,其类型称为KeyType

通过将KeyType放在数组声明的[]中来声明关联数组。关联数组的一个简单示例如下所示。

import std.stdio;

void main () { 
   int[string] e;      // associative array b of ints that are  
   
   e["test"] = 3; 
   writeln(e["test"]); 
   
   string[string] f; 
   
   f["test"] = "Tuts"; 
   writeln(f["test"]); 
   
   writeln(f);  
   
   f.remove("test"); 
   writeln(f); 
}

编译并执行上述代码后,将产生以下结果-

3 
Tuts 
["test":"Tuts"] 
[]

初始化关联数组

关联数组的简单初始化如下所示。

import std.stdio;

void main () { 
   int[string] days = 
      [ "Monday" : 0, 
         "Tuesday" : 1, 
         "Wednesday" : 2, 
         "Thursday" : 3, 
         "Friday" : 4, 
         "Saturday" : 5, 
         "Sunday" : 6 ]; 
   writeln(days["Tuesday"]);    
}

编译并执行上述代码后,将产生以下结果-

1

关联数组的属性

这是关联数组的属性-

Sr.No. Property & Description
1

.sizeof

Returns the size of the reference to the associative array; it is 4 in 32-bit builds and 8 on 64-bit builds.

2

.length

Returns number of values in the associative array. Unlike for dynamic arrays, it is read-only.

3

.dup

Create a new associative array of the same size and copy the contents of the associative array into it.

4

.keys

Returns dynamic array, the elements of which are the keys in the associative array.

5

.values

Returns dynamic array, the elements of which are the values in the associative array.

6

.rehash

Reorganizes the associative array in place so that lookups are more efficient. rehash is effective when, for example, the program is done loading up a symbol table and now needs fast lookups in it. Returns a reference to the reorganized array.

7

.byKey()

Returns a delegate suitable for use as an Aggregate to a ForeachStatement which will iterate over the keys of the associative array.

8

.byValue()

Returns a delegate suitable for use as an Aggregate to a ForeachStatement which will iterate over the values of the associative array.

9

.get(Key key, lazy Value defVal)

Looks up key; if it exists returns corresponding value else evaluates and returns defVal.

10

.remove(Key key)

Removes an object for key.

下面显示了使用上述属性的示例。

import std.stdio;

void main () { 
   int[string] array1;

   array1["test"] = 3; 
   array1["test2"] = 20; 
   
   writeln("sizeof: ",array1.sizeof); 
   writeln("length: ",array1.length); 
   writeln("dup: ",array1.dup);  
   array1.rehash; 
   
   writeln("rehashed: ",array1);  
   writeln("keys: ",array1.keys); 
   writeln("values: ",array1.values);
   
   foreach (key; array1.byKey) { 
      writeln("by key: ",key); 
   }

   foreach (value; array1.byValue) { 
      writeln("by value ",value); 
   }

   writeln("get value for key test: ",array1.get("test",10)); 
   writeln("get value for key test3: ",array1.get("test3",10));  
   array1.remove("test"); 
   writeln(array1); 
} 

编译并执行上述代码后,将产生以下结果-

sizeof: 8                                                                          
length: 2                                                                          
dup: ["test":3, "test2":20]                                                        
rehashed: ["test":3, "test2":20]                                                   
keys: ["test", "test2"]                                                            
values: [3, 20]                                                                    
by key: test                                                                       
by key: test2                                                                      
by value 3                                                                         
by value 20                                                                        
get value for key test: 3                                                          
get value for key test3: 10                                                        
["test2":20]