向RedBlackTree添加自定义类型

向RedBlackTree添加自定义类型,d,phobos,D,Phobos,我想保留一组有序的记录,标准为我提供了RedBlackTree。记录的类型是Tuple!(字符串,uint)。下面是它的样子: import std.json:parseJSON; uint[string]字表; 导入std.datetime.stopwatch:秒表,自动启动; 自动开关=秒表(自动启动。是); const auto j=parseJSON(get(link)); const long downloadTime=sw.peek.total!“MSEC”; 导入std.typec

我想保留一组有序的记录,标准为我提供了RedBlackTree。记录的类型是Tuple!(字符串,uint)。下面是它的样子:

import std.json:parseJSON;
uint[string]字表;
导入std.datetime.stopwatch:秒表,自动启动;
自动开关=秒表(自动启动。是);
const auto j=parseJSON(get(link));
const long downloadTime=sw.peek.total!“MSEC”;
导入std.typecons:元组,元组;
导入std.container.rbtree:RedBlackTree;
导入标准功能:binaryFun;
红黑树!(Tuple!(string,uint),binaryFun!(“a[1]>b[1]”)记录;
foreach(节点;j[“posts”].array()){
导入标准stdio:writeln;
导入标准utf:解码;
if(节点中的“com”){
导入标准算法:拆分器;
foreach(word;getStr(node[“com”].str()).splitter(“”)){
导入标准字符串:条带;
if(word.strip().length>0)
require(word,0)+;
记录~=(元组(word,wordTable[word]);//错误
}
}
}
现在我主要使用
insert()
方法将记录添加到记录中,但它会在运行时导致segfault。所以我决定使用~=希望得到更好的错误消息。下面是编译器所说的:

错误:无法追加类型元组!(字符串,uint)键入std.container.rbtree.RedBlackTree!(Tuple!(string,uint),binaryFun,false)

根据,我必须提供这样一个类型:对其调用
less(a,b)
将返回一个布尔值。所以我继续为它创建了一个类型:

struct记录{
字符串键;
单位价值;
int opCmp(参考常数记录其他)常数{
返回值-other.value;
}
}
//bool less(记录a、记录b){
//返回a0)
require(word,0)+;
记录~=(记录(word,wordTable[word]);
}
}
}
这一次,编译器抱怨:

错误:无法将类型记录附加到类型std.container.rbtree.RedBlackTree!(记录“a
因此,问题的要点是,如果我有一个带有自定义
binaryFun
的RedBlackTree,我如何向它添加元组或自定义类型的实例?

比较函数应该是字符串,而不是实际函数。因此,将初始代码中的
binaryFun!([a[1]>b[1])
替换为
“a[1]>b[1]”即可
RedBlackTree不会重载
~=
,因此您必须使用
insert
。比较函数应该是字符串,而不是实际函数。因此,将初始代码中的
binaryFun!(
b[1]”)替换为
“a[1]>b[1]”
RedBlackTree不会过载
~=
,因此您必须使用
insert