Copy std::stack into an std::vector
问题 Is the following code guaranteed by the standard to work(assuming st is not empty)? #include <vector> #include <stack> int main() { extern std::stack<int, std::vector<int> > st; int* end = &st.top() + 1; int* begin = end - st.size(); std::vector<int> stack_contents(begin, end); } 回答1: Yes. std::stack is just a container adapter. You can see that .top() is actually (§23.3.5.3.1) reference top() { return c.back(); } Where c is the container, which in this case is a std::vector Which means that