在阅读了 Andrei 的书后,我刚刚开始在 D 2.0 中实现我的第一个中等规模的程序 D 编程语言 .我遇到的第一个问题是使用带有内置关联数组的 std.algorithm 库。例如:
#!/usr/bin/env rdmd
import std.stdio;
import std.algorithm;
void main()
{
alias int[string] StringHashmap;
StringHashmap map1;
map1["one"] = 1;
map1["two"] = 2;
writefln("map1: %s", map1);
StringHashmap map2;
map2["two"] = 2;
map2["three"] = 3;
writefln("map2: %s", map2);
auto inter = setIntersection(map1, map2);
}
对我来说,这似乎是一件很简单的事情,期望在 inter 上迭代会产生单个“两个”条目。但是,我收到此编译器错误:./test.d(20): Error: template std.algorithm.setIntersection(alias less = "a < b",Rs...) if (allSatisfy!(isInputRange,Rs)) does not match any function template declaration
./test.d(20): Error: template std.algorithm.setIntersection(alias less = "a < b",Rs...) if (allSatisfy!(isInputRange,Rs)) cannot deduce template function from argument types !()(int[string],int[string])
我可以看到内置关联数组似乎没有提供与 std 算法一起使用的范围的任何版本。
我错过了什么吗?做错事了吗?如果没有,这是一个明显的遗漏吗?有什么原因为什么这不可用吗?
最佳答案
用这个:
auto inter = setIntersection(map1.keys, map2.keys);
关于d - 范围从 D 2 中的关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3435652/