How to move elements out of STL priority queue
问题 C++'s STL priority queue have a void pop() method, and a const ref top() method. Thus, if you want to move elements out of the queue, you have to do something like this: T moved = std::move(const_cast<T&>(myQueue.top()))); myQeue.pop(); This effectively casts the top to not a constant, so that it can be moved (rather than copied). I don't like this code, because the forced move may invalidate the invariants of the priority queue, which should not matter because of the pop, but things could go