当我在队列中弹出元素时它会被释放吗? [关闭]

问题描述 投票:0回答:1

我宁愿知道当我调用pop和push时内存是如何工作的?

c++ queue pop
1个回答
1
投票

该元素被删除并销毁。如果您对先前包含的元素持有引用(或指针),它将变为悬空,不得使用。

std::queue<int> bad {{1, 2, 3}};
const int &first = bad.front();
bad.pop();
// first is invalid at this point

底层容器可能会也可能不会将未使用的内存释放回OS。容器通常保持先前分配的内存准备好再次快速使用。见Why doesn't std::queue shrink its memory after popping elements?

© www.soinside.com 2019 - 2024. All rights reserved.