I am using the STL container for stack.

<include> stack;

I wish to check whether the integer is already in the stack or not before adding it into the stack. If it is in the stack then i do not need to add it. But if it is not in the stack then i need to add it. I do not want to change any element already in the stack during this operation.

I do not know how to do it using standard STL container for stack because there is no built in function which can do it.

Please let me know how can i do it.


Four "options" I can think of.

1) My first choice would be to use different type of container. I say this because I don't think of looping through a stack to evaluate the contents. To my knowledge the only element that is routinely available to view in a stack is the top. All the others are hidden from view until they become the top.

2) My second choice would be to create my own "stack" allowing me to loop through the stack without destroying it and rebuilding it.

3) My third choice would be to evaluate the top, push the value of the top to a second stack, then pop the top, and repeat until original stack is empty. Then the second stack would have to be popped and pushed back on the original stack to get it back to the original order.

4) Last, but not least, you could always try using an iterator to loop through the STL stack. Who knows, it might even work.