在阅读有关范围的文档后,我相信任何std.algorithm函数都应该适用于包含关联数组的任何容器,
但是下面的代码失败了:
double[double] freqIntensity;
foreach (complexval; resultfft)
{
freqIntensity[arg(complexval)]++;
}
minPos!("a > b")(freqIntensity);
Run Code Online (Sandbox Code Playgroud)
有错误:
src\main.d(56): Error: template std.algorithm.minPos does not match any function template declaration. Candidates are:
C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(6321): std.algorithm.minPos(alias pred = "a < b", Range)(Range range) if (isForwardRange!(Range) && !isInfinite!(Range) && is(typeof(binaryFun!(pred)(range.front, range.front))))
src\main.d(56): Error: template std.algorithm.minPos(alias pred = "a < b", Range)(Range range) if (isForwardRange!(Range) && !isInfinite!(Range) && is(typeof(binaryFun!(pred)(range.front, range.front)))) cannot deduce template function from argument types !("a > b")(double[double])
src\main.d(56): Error: template instance minPos!("a > b") errors instantiating template
Run Code Online (Sandbox Code Playgroud)
当然我可以迭代或做其他技巧,但我不想像在C++中那样犯错(比如在C++中编写C代码),我想,语言的功能对我有用.找到关联数组的最大值元素的最佳方法是什么.
关联数组不是范围,您必须传入值
import std.stdio, std.algorithm;
void main() {
double[double] freqIntensity = [1.0 : 2.0, 2.3 : 8.9];
writeln(minPos!("a > b")(freqIntensity.values));
}
Run Code Online (Sandbox Code Playgroud)