D语言多层嵌套数组结构实现 - 如何使用关联数组
在D语言中,可以使用嵌套的Associative Array(关联数组)来表示类似PHP的多层嵌套数组结构。以下是相应的D语言代码:
import std.stdio;
void main() {
auto testArr = [
'name': 'tom',
'age': 22,
'lover': [
'name': 'lily',
'age': '21',
'parent': [
'name': 'lily_parent',
'age': 50
]
]
];
writeln(testArr['name']); // 输出 'tom'
writeln(testArr['lover']['name']); // 输出 'lily'
writeln(testArr['lover']['parent']['name']); // 输出 'lily_parent'
}
在这个例子中,我们使用了嵌套的Associative Array来表示多层嵌套数组。D语言中的Associative Array可以使用类似PHP中的数组键值对的语法来访问其元素。在这个例子中,我们使用了多个嵌套的索引来访问各个元素。
原文地址: https://www.cveoy.top/t/topic/odoK 著作权归作者所有。请勿转载和采集!