What I’m trying to do is to construct a stack which contains unique elements.
And if an element is pushed to which is already in stack the element is not pushed but the existing elemetn should be moved to the top of the stack, i.e.
ABCD + B > ACDB
I would like to here from you which container will be the best choice to have this functionality.
I decided to user stack adapter over list, because
- list does provide constant time for element move
- list is one of the natively supported containers for the stack.
The drawback of my choice is that I have to manually check for the duplicate elements.
P.S. My compiler is not so recent so please don’t suggest unordered_set.
Basically you have to chose between constant time moving + long search, or constant time search + long moving.
It’s hard to say which would be more time-consuming, but consider this:
I’d suggest you to store elements and their stack positions in different containers. Store elements in a way that provides fast search, store stack positions in a way that provides fast movement. Connect both with pointers (so you can know which element is on which position, and which position holds which element <– messy phrase, I know it!), and you will perform stuff rather fast.