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
2010-06-13 18:57:40
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
2010-06-13 18:59:30
@Stephen: Except `deque` has no such function. :)
GMan
2010-06-13 19:27:46
@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
2010-06-13 23:15:31
+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
2010-06-13 18:58:03
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
2010-06-13 19:00:43
@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
2010-06-13 19:02:07
@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
2010-06-13 19:04:10
@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
2010-06-13 19:08:41
@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
2010-06-13 19:08:44
@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
2010-06-13 19:19:05
@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
2010-06-13 19:28:34
@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
2010-06-13 19:29:34
"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
2010-06-13 19:34:34
@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
2010-06-13 19:39:06
+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
2010-06-13 19:01:21
@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
2010-06-13 19:02:38
@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
2010-06-13 19:05:35
@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
2010-06-13 19:08:55
@Neil - actually yes you are right. I should be using a `deque` instead.
BeeBand
2010-06-13 19:10:56
"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
2010-06-13 19:30:05
+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
2010-06-13 22:58:15
+1: I didn't know that it was required that the container be stored as a protected member named `c`.
James McNellis
2010-06-14 12:53:55