Skip to content

Commit d0b7a8f

Browse files
authored
fix: 在泛型这一节中,堆的使用里面缺少NewHeap函数,补充了NewHeap函数 (#38)
* 在泛型这一节中,堆的使用里面缺少NewHeap函数,补充了NewHeap函数 * 测试 * modified: README.md
1 parent 348dade commit d0b7a8f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/essential/senior/90.generic.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
date: 2022-08-26
33
---
4-
54
# 泛型
65

76
泛型,或者更学术化的名称 —— 参数化多态(Parameterized
@@ -250,10 +249,6 @@ func Assert[T any](v any) (bool, T) {
250249
}
251250
```
252251

253-
254-
255-
256-
257252
## 类型集
258253

259254
在 1.18 以后,接口的定义变为了类型集 `(type set)`,含有类型集的接口又称为 `General interfaces` 即通用接口。
@@ -724,6 +719,14 @@ func (heap *BinaryHeap[T]) down(i int) {
724719
func (heap *BinaryHeap[T]) Size() int {
725720
return len(heap.s)
726721
}
722+
723+
func NewHeap[T any](n int, c Comparator[T]) BinaryHeap[T] {
724+
var heap BinaryHeap[T]
725+
heap.s = make([]T, 0, n)
726+
heap.Comparator = c
727+
return heap
728+
}
729+
727730
```
728731

729732
使用起来如下

0 commit comments

Comments
 (0)