d语言常用mapfilterspillerjoin等用法给出实际运行例子
- 使用map函数将数组中的元素进行平方操作:
int[] arr = [1,2,3,4,5];
int[] result = arr.map!(a => a*a).array;
// result为 [1, 4, 9, 16, 25]
- 使用filter函数将数组中的偶数筛选出来:
int[] arr = [1,2,3,4,5];
int[] result = arr.filter!(a => a % 2 == 0).array;
// result为 [2, 4]
- 使用split函数将字符串按照空格分割成数组:
string str = "hello world";
string[] result = str.split();
// result为 ["hello", "world"]
- 使用join函数将数组中的元素以逗号连接成字符串:
int[] arr = [1,2,3,4,5];
string result = arr.map!(a => a.to!string).join(",");
// result为 "1,2,3,4,5"
``
原文地址: http://www.cveoy.top/t/topic/fyOL 著作权归作者所有。请勿转载和采集!