2011-06-02 133 views
2

的火卫一的文档显示传递给可变参数函数传递一个范围范围的可变参数函数

int[] a = [ 1, 2, 4, 5, 7, 9 ]; 
int[] b = [ 0, 1, 2, 4, 7, 8 ]; 
int[] c = [ 0, 1, 4, 5, 7, 8 ]; 
assert(equal(setIntersection(a, a), a)); 
assert(equal(setIntersection(a, b), [1, 2, 4, 7][])); 
assert(equal(setIntersection(a, b, c), [1, 4, 7][])); 

但如果你有一系列范围的下列范围例子,你不知道在提前多少元素它包含像

int[][] a = [[1,2,3,4],[1,2,4,5],[1,3,4,5]]; 

我能想到的唯一的事情就是

if (a.length > 1) { 
    auto res = array(setIntersection(a[0], a[1])); 
    for (int i = 2; i < a.length; i++) 
     res = array(setIntersection(res, a[i])); 
    writeln(res); 
} 

哪些工作。但我希望能够直接将参数传递给函数,比如setIntersection(a.tupleof)或类似的东西(我知道tupleof在这里不起作用)。

回答

2

,如果你不知道有多少元素a将你将不能够把它扩展到在编译时的元组(并因此它传递到一个函数)

,这样的循环是你的最好的选择(或实施你自己的setIntersection,可以采取一系列的范围)