优先级队列返回错误顺序
来源:stackoverflow
2024-04-01 23:57:33
0浏览
收藏
欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《优先级队列返回错误顺序》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!
问题内容
我编辑了一些关于优先级队列的官方文档,发现了一个有趣的行为:顺序被打乱了。
具体来说,我更新了这一行:
item := heap.pop(&pq).(*item)
使用这一行(现在指向实现的方法本身)
item := pq.pop().(*item)
现在的结果是: 03:香蕉 02:苹果 04:梨
为什么结果不同?如果这两个方法(heap.pop 和 *pq.pop)执行不同的功能,为什么它们的名称相同?它会导致混乱。
package main
import (
"container/heap"
"fmt"
)
// An Item is something we manage in a priority queue.
type Item struct {
value string // The value of the item; arbitrary.
priority int // The priority of the item in the queue.
// The index is needed by update and is maintained by the heap.Interface methods.
index int // The index of the item in the heap.
}
// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
// We want Pop to give us the highest, not lowest, priority so we use greater than here.
return pq[i].priority > pq[j].priority
}
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *PriorityQueue) Push(x interface{}) {
n := len(*pq)
item := x.(*Item)
item.index = n
*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
item.index = -1 // for safety
*pq = old[0 : n-1]
return item
}
// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value string, priority int) {
item.value = value
item.priority = priority
heap.Fix(pq, item.index)
}
// This example creates a PriorityQueue with some items, adds and manipulates an item,
// and then removes the items in priority order.
func main() {
// Some items and their priorities.
items := map[string]int{
"banana": 3, "apple": 2, "pear": 4,
}
// Create a priority queue, put the items in it, and
// establish the priority queue (heap) invariants.
pq := make(PriorityQueue, len(items))
i := 0
for value, priority := range items {
pq[i] = &Item{
value: value,
priority: priority,
index: i,
}
i++
}
heap.Init(&pq)
// Take the items out; they arrive in decreasing priority order.
for pq.Len() > 0 {
//item := heap.Pop(&pq).(*Item)
item := pq.Pop().(*Item)
fmt.Printf("%.2d:%s ", item.priority, item.value)
}
}正确答案
如果你仔细观察,priorityqueue实际上只是一个[]*item。文档中定义 // priorityqueue实现heap.interface并保存items。这个堆接口定义在documentation中:
// Note that Push and Pop in this interface are for package heap's
// implementation to call. To add and remove things from the heap,
// use heap.Push and heap.Pop.
type Interface interface {
sort.Interface
Push(x interface{}) // add x as element Len()
Pop() interface{} // remove and return element Len() - 1.
}
很明显,优先级队列使用堆来正常工作。 heap 有 push 和 pop 方法,然后使用 priorityqueue 定义的方法对队列中的 item 进行排序、存储和优先级。
您所做的只是显示您存储的列表。在此过程中没有发生排序和优先级。
以上就是《优先级队列返回错误顺序》的详细内容,更多关于的资料请关注golang学习网公众号!
版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
无法部署使用外部库的运行时 1.12 的 AppEngine Go 应用程序
- 上一篇
- 无法部署使用外部库的运行时 1.12 的 AppEngine Go 应用程序
- 下一篇
- 被误解的「中文版Sora」背后,字节跳动有哪些技术?
查看更多
最新文章
-
- Golang · Go问答 | 3天前 | go · TypeSymlink Go archive/tar链接条目 Tar硬链接 Tar符号链接 Header.Linkname TypeLink
- Go archive/tar识别 Tar 硬链接与符号链接的排查指南
- 459浏览 收藏
-
- Golang · Go问答 | 3天前 |
- Go archive/tar读取 PAX 扩展头字段的兼容方法
- 487浏览 收藏
-
- Golang · Go问答 | 3天前 | go · Go archive/zip Go Zip重复条目 Go Writer.Create Go压缩包去重 Go Zip覆盖策略
- Go archive/zip写入 Zip 时处理重复条目的实现方法
- 435浏览 收藏
-
- Golang · Go问答 | 3天前 | 文件读取 · Go问答 · Go archive/zip File.Open OpenReader Zip读取
- Go archive/zip读取 Zip 文件并及时关闭的资源方案
- 175浏览 收藏
-
- Golang · Go问答 | 3天前 | go · Go archive/zip文件名校验 Go ZIP路径安全 Go压缩包目录穿越防护 Go ErrInsecurePath Go解压文件名检查
- Go archive/zip校验压缩包内文件名的安全边界
- 187浏览 收藏
-
- Golang · Go问答 | 3天前 | go · 数据安全 · 持久化 临时文件 原子替换 Go os.File
- Go os.File写入临时文件后原子替换的持久化方案
- 448浏览 收藏
-
- Golang · Go问答 | 3天前 | 错误处理 · 事务 · database/sql · Go问答 · Go database/sql Tx Go事务重试 Go死锁处理 Go序列化冲突 database/sql错误分类
- Go database/sql Tx区分可重试冲突与业务错误的处理边界
- 218浏览 收藏
-
- Golang · Go问答 | 3天前 | go · database/sql · Go 事务 database/sql Tx
- Go database/sql Tx把事务边界放到业务操作外层的设计方法
- 215浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 101次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 183次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 125次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 105次使用
-
- CMMLU
- 深入了解CMMLU中文评估基准,涵盖67个学科主题,提供数据集下载、Zero-shot/Five-shot评估方法及排行榜,助力优化中文语言模型性能。
- 83次使用
查看更多
相关文章
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go select 用 time.After 做超时有什么资源代价
- 2026-09-10 501浏览
-
- Go 取 range 变量地址为什么得到重复指针
- 2026-09-07 501浏览
-
- Go net.Conn 写入超时为何仍会卡住:SetWriteDeadline、部分写入与连接复用检查
- 2026-08-30 501浏览

