dlang语言dmd2103版本以下代码如果去掉第2行import stdalgorithm会报错提示:Error no property reverse for two of type int我的代码如下:import stdstdio;import stdalgorithm;void main int3 one = 123; auto two = one$-2$; rever
?
原因是:Array类型没有reverse方法(即int[]类型没有reverse方法),需要通过std.algorithm.reverse()函数来实现反转数组。
在第2行添加import std.algorithm后,可以直接调用两种方式来实现数组反转:
- 使用std.algorithm.reverse()函数,如下所示:
reverse(two);
- 使用Array类型的opApply方法,如下所示:
two.reverse();
两者的区别在于:std.algorithm.reverse()函数是对传入的数组进行反转操作,并返回反转后的数组;而Array类型的opApply方法是在原数组上进行反转操作,并没有返回任何值。
原文地址: https://www.cveoy.top/t/topic/e9ci 著作权归作者所有。请勿转载和采集!