c++ - 从 C++ priority_queue 释放

标签 c++ memory-management

删除 std::priority_queue 中的元素并在之后弹出它是否安全,或者弹出是否尝试对引用做任何事情而不是丢弃它?

  while(!priority_queue.empty()){
    delete priority_queue.top();
    priority_queue.pop();
  }

最佳答案

在 OP 澄清说队列不包含任何指针,只包含对象之后:

您根本不必调用delete。只需在您的 while 循环中调用 pop() 即可。容器处理释放。

关于c++ - 从 C++ priority_queue 释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55298682/

相关文章:

c - 如何在动态分配的内存中移动参数?

c++ - muQueue.front()给出const实例,即使我没有将其标记为const

c++ - 从主框架窗口标题中删除 "Untitled - "

c++ - 递归查找优化

c++ - 在堆栈上分配对齐的内存,如 _alloca

iphone - didReceiveMemoryWarning 和 viewDidUnload

c++ - FLTK GUI 不显示 (Dev-C++)

c++ - 将 C++ 方法参数转换为模板参数失败并出现编译错误

linux - 为什么我的进程需要很长时间才能终止?

iOS 应用程序在方法调用时崩溃