The C++ standard library tries to have as uniform of an interface as possible for all of its containers. However, one black sheep sticks out:
#include <algorithm>
#include <stack>
int main()
{
std::stack<int> s;
std::reverse(std::begin(s), std::end(s));
}
I can't use this common idiom to reverse a container because a stack is not a container: it's an adapter. As a result, it has no begin
or end
functions.
I would have to do something like this:
std::stack<int> s;
std::stack<int> s2;
while (!s.empty())
{
s2.push(s.top());
s.pop();
}
while (!s2.empty())
{
s2.top(); // do something with it
s2.pop();
}
What is the "standard" way of doing this? No ad-hoc hacks or convoluted methods, please.