http://play.golang.org/p/icQO_bAZNE
我正在练习使用堆排序,但是
prog.go:85: type bucket is not an expression
prog.go:105: cannot use heap.Pop(bucket[i].([]IntArr)) (type interface {}) as type int in assignment: need type assertion
[process exited with non-zero status]
我收到这些错误,并且不知道如何正确键入断言
问题出在以下几行:
heap.Push(bucket[x].([]IntArr), elem)
arr[index] = heap.Pop(bucket[i].([]IntArr))
因为我想使用堆结构来从每个存储桶中提取值
每个桶都是[]IntArr
并且IntArr
是[]int
,如下所示
type IntArr []int
type bucket [10]IntArr
周末我尝试了很多方法,但无法弄清楚,我非常感激。
最佳答案
要使用堆包,您应该为您的类型实现 heap.Interface (在本例中,为您的 IntArr 类型)。您可以在这里找到示例:http://golang.org/pkg/container/heap/#pkg-examples
然后你可以做类似的事情
heap.Push(bucket[x], elem)
关于types - 去Go语言: Type assertion on customized type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20752772/