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 eg 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.
Ie other approaches are fairly unnatural & construed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.