c++ - Copy std::stack into an std::vector -
is following code guaranteed standard work(assuming st 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); }
yes.
std::stack container adapter.
you can see .top() (§23.3.5.3.1)
reference top() { return c.back(); } where c container, in case std::vector
which means code translated into:
extern std::vector<int> st; int* end = &st.back() + 1; int* begin = end - st.size(); std::vector<int> stack_contents(begin, end); and std::vector guaranteed continuous there should no problem.
however, not mean idea. if need use "hacks" indicator of bad design. want use std::vector beginning.
Comments
Post a Comment