With a simple stack, there is no better way.
Interestingly, the stack adapter actually provides the underlying container as a protected element. This means you can do this:
template <typename T, typename Container = std::deque<T>> class stack_ex : public std::stack<T, Container> { public: using stack_ex::stack::stack; void swap_top() { auto last = c.rbegin(); auto before_last = std::prev(last); std::iter_swap(last, before_last); } };
source share