How exactly I can get the max_element of a stack ? STL stack doesn't any begin()
or end()
method that I can get the maximum by follow:
auto max = max_element(c.begin(), c.end());
How exactly I can get the max_element of a stack ? STL stack doesn't any begin()
or end()
method that I can get the maximum by follow:
auto max = max_element(c.begin(), c.end());
A std::stack
has a restricted interface, which is the whole point of that abstraction. If not then you could just have used e.g. a std::deque
. But you have a number of options:
You can pop all items. If you desire the original stack back at the end then you can just push them back.
You can access the underlying container (without using a derived class). It's a protected member. The infamous C++ type system loophole for member pointers is helpful, if you're afraid of casting and formally undefined behavior.
You can use a custom derived class instead of std::stack
directly.
This list is not exhaustive but they are the more natural options.
I.e. other approaches are fairly unnatural & construed.