我宁愿知道当我调用pop和push时内存是如何工作的?
该元素被删除并销毁。如果您对先前包含的元素持有引用(或指针),它将变为悬空,不得使用。
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?。