tags:

views:

128

answers:

4

I want to be able to convert a std::stack<> to a std::deque<>. Is there a straightforward conversion?

+4  A: 

You'll need to do it manually:

while (!stk.empty())
{
    deq.push_back(stk.top());
    stk.pop();
}
R Samuel Klatchko
Ah, ok - thanks. Was just wondering if I could do it without having to make a copy of the stack, and popping it into a deque.
BeeBand
@Stephen: Except `deque` has no such function. :)
GMan
@GMan: Holy crap, you're right. Makes sense, given that you wouldn't know which way to grow it. I guess you could create the deque with `std::deque deq(stk.size());`, but then you can't use `push_back`.
Stephen
+2  A: 

You need to pop all the elements off of the stack and into a different container.

If you need to do this, perhaps std::stack is the wrong data structure for your use case.

James McNellis
Well - its so that I can compare stacks. The `equal` function in the standard lib, needs a container with `begin()` and `end()` functions. And I need to easily see the reverse of my stacks as well, so I thought about converting them to deques.
BeeBand
@BeeBand: Why not just use a `std::deque` instead of a `std::stack` then? It can be used for the same purpose, just instead of `top()`, `push()`, and `pop()`, you need to use `back()`, `push_back()`, and `pop_back()`.
James McNellis
@James - because the pop, push interface is very useful. I need `stack` functionality (i.e. `push`, `pop`, and `top` ) and don't want to call `push_back()` or `pop_back()`.
BeeBand
@BeeBand: But you're *not* just using `stack` functionality. If you need to see elements in reverse, it's not a stack. Just use a `deque`.
GMan
@BeeBand: The naming of member functions is an odd reason to choose one data structure over another. Given your use case, `std::stack` is the wrong data structure. The right answer is to use a `std::deque`. If the names _really_ bother you that much, then write non-member wrapper functions that do what you want; you can call those whatever you'd like.
James McNellis
@James. Hmm. Yeah. I think you're right. I need a `deque`!
BeeBand
@James... re. non member wrapper, how might I do that for a `deque`- do I not need to write an entire adaptor around a `deque`? I didn't realise you could just wrap up functions in the STL independently.. sorry - probably a whole other question in itself.
BeeBand
@BeeBand: Just write a function template `top()` that takes a deque as a parameter and returns the result of calling `pop_back()` on the deque.
James McNellis
@BeeBand: I think he means for example `template <typename T, typename U> void push(T }` etc. But by all means you could take the source for `stack` in your implementation, and use it as the basis to write your own complete container adaptor, with `push`, `pop`, `top`, `begin` and `end`.
Steve Jessop
"its so that I can compare stacks" - if you're just comparing stacks (of the same type), then they have an `operator==` overload. Comparing a stack to some other container is the tricky part, or seeing it in reverse.
Steve Jessop
@Steve, hmm. I didn't realise `stack` had an `operator==` overload. Well yes, actually I am going to have to iterate over the stacks also, a container adaptor might be a good idea in that case.
BeeBand
+2  A: 

I think if you need this, you would be better off using deque in the first place, instead of a stack. There is no way of getting at the underlying storage used by the stack adaptor, if that is what you are asking.

anon
@Neil, that's exactly what I was asking yes. I see. well. The stack interface fits my needs - its just that I need to be able to compare the items in the stacks.
BeeBand
@BeeBand Really, a deque pretty much works like a stack anyway - you just say pop_front and push_back instead of pop and push.
anon
@Neil - this is true. I just like the small function names, and the idea of `back` and `front` make things confusing for what I'm doing.
BeeBand
@Neil - actually yes you are right. I should be using a `deque` instead.
BeeBand
"The stack interface fits my needs - its just that..." this is another way of saying, "The stack interface does not fit my needs" ;-)
Steve Jessop
@Steve. :) yes ok this is true, hehe.
BeeBand
+3  A: 

It is possible to access the underlying container without copying the data, but it requires a certain amount of evil. The container is exposed as a protected member, called c, which allows shenanigans such as this:

template <typename T>
class Shenanigans : private stack<T>
{
public:
    explicit Shenanigans(stack<T>& victim) : victim(victim)
    {
        swap(victim);
    }

    ~Shenanigans()
    {
        swap(victim);
    }

    using stack<T>::c;

private:
    stack<T>& victim;
};

int main()
{
    stack<int> s;
    s.push(42);

    {
        Shenanigans<int> sh(s);
        // The deque is accessible as sh.c, but the stack is temporarily empty.
        cout << "Size: " << s.size() << " Data: " << sh.c.front() << "\n";
    }

    // The stack is restored.
    cout << "Size: " << s.size() << " Data: " << s.top() << "\n";
}

Of course a far, far better solution is to choose a container that meets your needs.

Mike Seymour
+1: I didn't know that it was required that the container be stored as a protected member named `c`.
James McNellis