问题在于 rowstring[string] 类型,而 canFind 函数的参数类型是 string,所以编译器无法匹配函数调用。可以改为使用 in 关键字来判断 key 是否在 row 的键中出现:

T[] array_column(T, U)(T[U][] array, U key)
{
    T[] result;
    foreach (row; array)
    {
        if (key in row.keys)
        {
            result ~= row[key];
        }
    }
    return result;
}

这里使用 row.keys 来获取 row 的所有键,并使用 in 来判断 key 是否在其中出现。

以下D语言代码import stdstdio;import stdalgorithm;T array_columnT UTU array U key T result; foreach row; array if rowcanFindkey result ~= rowkey; return res

原文地址: https://www.cveoy.top/t/topic/fuUH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录