Why we need vector, queue, stack if we already have double ended queue in cpp ? | Sololearn: Learn to code for FREE!

+4

Why we need vector, queue, stack if we already have double ended queue in cpp ?

9/27/2021 6:38:27 AM

Aditya Singh

13 Answers

New Answer

+4

Well stack is also in STL. The difference between them is that in stack you have just push and pop(stack data structure) but dequeue is more like a vector, where u can go to any element of it or even insert().. basically you can use dequeue for anything u use stack for. It's just that some people find it easier and less confusing to use stack when you only need pop() and push()...

+4

A stack and a queue are not the same thing. They are different data structures with different uses. A stack is a last in first out (LIFO) data structure. A queue is a first in first out (FIFO) data structure. A double ended queue acts more like a vector with efficient insertion and removal at either end. A double ended queue is not a stack but you could use it like one. Just as an electric drill is not a hammer but you could use it as one. Different data structures are more suited to different tasks. It's like asking why do different types of saws exist when you have a hacksaw.

+2

Aleksei Radchenov, your first answer was spot on and I have no issue with that. It was the comments such as... "Stack is just one example of double ended queue" and "Mate, stack is a double ended queue" - no it isn't. it can be implemented using a deque but a stack has less functionality than a deque as you yourself originally stated... "It's just that some people find it easier and less confusing to use stack when you only need pop() and push()" It also provides more code reliability since you can't accidentally call something you shouldn't because intellisense or code completion suggested it. The stack could also be implemented using a vector or list as well, in which case deque doesn't even come into the equation. "queue might be both LIFO and FIFO." - no the C++ queue class is a FIFO. Again the underlying container and the methods offered by the queue are not the same. The queue restricts you to a limited number of operations that only apply to a queue. Again a queue can be implemented using either a list or a deque. This is the whole point of abstract data types. It is this abstraction that allows us to concentrate on the exposed functionality and not the underlying implementation.

+2

LIFO access is natural via a stack

+1

STL dude we have dequeue<int>de; in dequeue we have both pop_front() and pop_back() , front() and back()

+1

Mate STL is a library ;)

+1

@Aleksei Radchenkov, A stack is a LIFO data structure and the stack class in the C++ STL in particular is a LIFO. It only has the methods push and pop to insert and remove elements from the top of the stack. The underlying implementation of the stack class in the C++ STL uses a deque unless a vector or list is specified. https://www.cplusplus.com/reference/stack/stack/ https://en.wikipedia.org/wiki/Stack_(abstract_data_type) https://en.wikipedia.org/wiki/Queue_(abstract_data_type)

+1

Martin Taylor, yeah I totally know that... How is it different from what I said?

+1

Martin Taylor , right I've confused some things.... Thanks for explanation bro.

0

Mate, stack is a double ended queue... Not sure what u mean by "we already have double ended queue in CPP" but there is a std::<stack> header in C++.

0

Google, dude!

0

And I'm asking about STL.

0

Martin Taylor, queue might be both LIFO and FIFO. Stack is just one example of double ended queue. As you said double ended queue is not a stack but stack is a specific type of double ended queue....