container/heap包的使用

package main
import (
	"fmt"
	"container/heap"
)
type heap_ []int
func (h heap_)Len() int {
	return len(h)
}
func (h heap_)Less(i,j int) bool {
	return h[i]<h[j]
}
func (h heap_)Swap(i,j int) {
	h[i], h[j] = h[j], h[i]
}
func (h *heap_)Push(val interface{}) {
	*h = append(*h, val.(int))
}
func (h *heap_)Pop() interface{} {
	ret := (*h)[h.Len()-1]
	*h = (*h)[:h.Len()-1]
	return ret
}
func main() {
	h := &heap_{}
	heap.Init(h)
	heap.Push(h,3)
	heap.Push(h,7)
	heap.Push(h,5)
	heap.Push(h,2)
	fmt.Println(h)
	heap.Pop(h)
	fmt.Println(h)
	heap.Init(h)
	fmt.Println(h)
}

你可能感兴趣的:(Golang,container)