Why does C++ have queue and stack given it already have deque? [duplicate]
up vote
1
down vote
favorite
This question already has an answer here:
c++ deque vs queue vs stack
8 answers
I am wondering why does C++ have queue and stack given it already have deque.
It seems that the runtime of the stack/queue and using deque to simulate stack/queue is the same. Also, the deque supports the modifiers like erase, iterators and random access, which neither the stack or queue supports.
So why do C++ provide all three of them given that the deque is much more powerful than the rest of the two?
Thanks!
c++ data-structures stl deque
marked as duplicate by Caleth, Baum mit Augen
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 18 at 20:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 1 more comment
up vote
1
down vote
favorite
This question already has an answer here:
c++ deque vs queue vs stack
8 answers
I am wondering why does C++ have queue and stack given it already have deque.
It seems that the runtime of the stack/queue and using deque to simulate stack/queue is the same. Also, the deque supports the modifiers like erase, iterators and random access, which neither the stack or queue supports.
So why do C++ provide all three of them given that the deque is much more powerful than the rest of the two?
Thanks!
c++ data-structures stl deque
marked as duplicate by Caleth, Baum mit Augen
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 18 at 20:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
They're not mutually exclusive. In fact, bothstd::stack
andstd::queue
usestd::deque
by default.
– chris
Nov 9 at 14:12
stackoverflow.com/questions/2247982/…
– Mat
Nov 9 at 14:15
1
Sometimes less is more. It's much easier to understand the role in the program of a stack than of a deque that's being used as if it were a stack.
– Pete Becker
Nov 9 at 14:17
1
A type's "power" is often synonymous with risk of use. More "powerful" features make it harder to detect errors since by definition there are fewer things you aren't allowed to do. A prime example of this is c style casts. They are immensely powerful in what casts they can perform they may replace any of the c++ style casts. But their power also means they are dangerous to use. So despite the fact that they make all other casts redundant, their use is discouraged specifically because it's so powerful. Rather than viewingqueue
andstack
being "less powerful", look at them as being safer.
– François Andrieux
Nov 9 at 14:23
2
@FrançoisAndrieux nicely put, i'd add that usingqueue
andstack
is more expressive. Code is much easier to reason about when a stack is astack
and a queue is aqueue
– user463035818
Nov 9 at 15:20
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This question already has an answer here:
c++ deque vs queue vs stack
8 answers
I am wondering why does C++ have queue and stack given it already have deque.
It seems that the runtime of the stack/queue and using deque to simulate stack/queue is the same. Also, the deque supports the modifiers like erase, iterators and random access, which neither the stack or queue supports.
So why do C++ provide all three of them given that the deque is much more powerful than the rest of the two?
Thanks!
c++ data-structures stl deque
This question already has an answer here:
c++ deque vs queue vs stack
8 answers
I am wondering why does C++ have queue and stack given it already have deque.
It seems that the runtime of the stack/queue and using deque to simulate stack/queue is the same. Also, the deque supports the modifiers like erase, iterators and random access, which neither the stack or queue supports.
So why do C++ provide all three of them given that the deque is much more powerful than the rest of the two?
Thanks!
This question already has an answer here:
c++ deque vs queue vs stack
8 answers
c++ data-structures stl deque
c++ data-structures stl deque
edited Nov 9 at 14:14
asked Nov 9 at 14:11
Major
113
113
marked as duplicate by Caleth, Baum mit Augen
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 18 at 20:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Caleth, Baum mit Augen
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 18 at 20:06
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
They're not mutually exclusive. In fact, bothstd::stack
andstd::queue
usestd::deque
by default.
– chris
Nov 9 at 14:12
stackoverflow.com/questions/2247982/…
– Mat
Nov 9 at 14:15
1
Sometimes less is more. It's much easier to understand the role in the program of a stack than of a deque that's being used as if it were a stack.
– Pete Becker
Nov 9 at 14:17
1
A type's "power" is often synonymous with risk of use. More "powerful" features make it harder to detect errors since by definition there are fewer things you aren't allowed to do. A prime example of this is c style casts. They are immensely powerful in what casts they can perform they may replace any of the c++ style casts. But their power also means they are dangerous to use. So despite the fact that they make all other casts redundant, their use is discouraged specifically because it's so powerful. Rather than viewingqueue
andstack
being "less powerful", look at them as being safer.
– François Andrieux
Nov 9 at 14:23
2
@FrançoisAndrieux nicely put, i'd add that usingqueue
andstack
is more expressive. Code is much easier to reason about when a stack is astack
and a queue is aqueue
– user463035818
Nov 9 at 15:20
|
show 1 more comment
1
They're not mutually exclusive. In fact, bothstd::stack
andstd::queue
usestd::deque
by default.
– chris
Nov 9 at 14:12
stackoverflow.com/questions/2247982/…
– Mat
Nov 9 at 14:15
1
Sometimes less is more. It's much easier to understand the role in the program of a stack than of a deque that's being used as if it were a stack.
– Pete Becker
Nov 9 at 14:17
1
A type's "power" is often synonymous with risk of use. More "powerful" features make it harder to detect errors since by definition there are fewer things you aren't allowed to do. A prime example of this is c style casts. They are immensely powerful in what casts they can perform they may replace any of the c++ style casts. But their power also means they are dangerous to use. So despite the fact that they make all other casts redundant, their use is discouraged specifically because it's so powerful. Rather than viewingqueue
andstack
being "less powerful", look at them as being safer.
– François Andrieux
Nov 9 at 14:23
2
@FrançoisAndrieux nicely put, i'd add that usingqueue
andstack
is more expressive. Code is much easier to reason about when a stack is astack
and a queue is aqueue
– user463035818
Nov 9 at 15:20
1
1
They're not mutually exclusive. In fact, both
std::stack
and std::queue
use std::deque
by default.– chris
Nov 9 at 14:12
They're not mutually exclusive. In fact, both
std::stack
and std::queue
use std::deque
by default.– chris
Nov 9 at 14:12
stackoverflow.com/questions/2247982/…
– Mat
Nov 9 at 14:15
stackoverflow.com/questions/2247982/…
– Mat
Nov 9 at 14:15
1
1
Sometimes less is more. It's much easier to understand the role in the program of a stack than of a deque that's being used as if it were a stack.
– Pete Becker
Nov 9 at 14:17
Sometimes less is more. It's much easier to understand the role in the program of a stack than of a deque that's being used as if it were a stack.
– Pete Becker
Nov 9 at 14:17
1
1
A type's "power" is often synonymous with risk of use. More "powerful" features make it harder to detect errors since by definition there are fewer things you aren't allowed to do. A prime example of this is c style casts. They are immensely powerful in what casts they can perform they may replace any of the c++ style casts. But their power also means they are dangerous to use. So despite the fact that they make all other casts redundant, their use is discouraged specifically because it's so powerful. Rather than viewing
queue
and stack
being "less powerful", look at them as being safer.– François Andrieux
Nov 9 at 14:23
A type's "power" is often synonymous with risk of use. More "powerful" features make it harder to detect errors since by definition there are fewer things you aren't allowed to do. A prime example of this is c style casts. They are immensely powerful in what casts they can perform they may replace any of the c++ style casts. But their power also means they are dangerous to use. So despite the fact that they make all other casts redundant, their use is discouraged specifically because it's so powerful. Rather than viewing
queue
and stack
being "less powerful", look at them as being safer.– François Andrieux
Nov 9 at 14:23
2
2
@FrançoisAndrieux nicely put, i'd add that using
queue
and stack
is more expressive. Code is much easier to reason about when a stack is a stack
and a queue is a queue
– user463035818
Nov 9 at 15:20
@FrançoisAndrieux nicely put, i'd add that using
queue
and stack
is more expressive. Code is much easier to reason about when a stack is a stack
and a queue is a queue
– user463035818
Nov 9 at 15:20
|
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
7
down vote
std::queue
and std::stack
aren't actually containers in the standard library. They are container adaptors and exist to give you an specific interface on top of an actual container.
You wouldn't want your stack to have an operator
so we wrap std::deque
(by default, you could use a different container) so we don't expose operations a stack wouldn't have.
add a comment |
up vote
3
down vote
std::stack
and std::queue
are so-called "container adapters", as in they adapt the interface of the wrapped container (a template parameter, defaulted to std::deque
, but could be anything that implements back()
, push_back()
and pop_back()
for std::stack
, and back()
, pop_front()
and push_back()
for std::queue
) to the restricted interface of a stack or a queue; if you prefer, they dumb down the underlying container interface.
The general idea should be that the "restricted" container provides just the operations of the "expected mode of operation" of the container - an std::stack
allows only stack-like operations, unlike the underlying std::deque
that allows, for example, random insert/access. Another advantage is that compile-time polymorphism over the underlying container type is a little more comfortable than passing the plain container (you may get better error messages).
In practice, I always found these adapters at least useless, more often just hindrances:
- they don't restrict the space of available states, as, given extra space, both an
std::stack
andstd::queue
can be mutated in any way; - they don't provide type erasure over the underlying container type, as it's a template parameter, so they aren't useful to write a non-template function that takes any stack, regardless of the underlying container;
- most often their interface is way too restricted; you cannot even do a debug print of the full content of an
std::stack
, even if the underlying container fully supported forward iterators (or even random access).
Your second bullet point is easy to overcome by adding an extra template parameter to the function: coliru.stacked-crooked.com/a/79028b2d002e58fe. The other points though are very valid.
– NathanOliver
Nov 9 at 14:35
@NathanOliver: that's exactly the case I mention in my second paragraph, about compile-time polymorphism. But that's not the same as "regular" polymorphism, as it infects everything with templates, and the only advantage it provides over having the function accepting a generic container is that it provides better error messages.
– Matteo Italia
Nov 9 at 14:37
add a comment |
up vote
1
down vote
Both, std::stack
and std::queue
are container adaptors. The use a std::deque
as the backing container by default, but thats not necessarily the case.
Both, stack and queue, have a reduced interface compared to a deque. Hence, you might want to have a stack that does not use a std::deque
but some other container and this is what the adaptors are for.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
std::queue
and std::stack
aren't actually containers in the standard library. They are container adaptors and exist to give you an specific interface on top of an actual container.
You wouldn't want your stack to have an operator
so we wrap std::deque
(by default, you could use a different container) so we don't expose operations a stack wouldn't have.
add a comment |
up vote
7
down vote
std::queue
and std::stack
aren't actually containers in the standard library. They are container adaptors and exist to give you an specific interface on top of an actual container.
You wouldn't want your stack to have an operator
so we wrap std::deque
(by default, you could use a different container) so we don't expose operations a stack wouldn't have.
add a comment |
up vote
7
down vote
up vote
7
down vote
std::queue
and std::stack
aren't actually containers in the standard library. They are container adaptors and exist to give you an specific interface on top of an actual container.
You wouldn't want your stack to have an operator
so we wrap std::deque
(by default, you could use a different container) so we don't expose operations a stack wouldn't have.
std::queue
and std::stack
aren't actually containers in the standard library. They are container adaptors and exist to give you an specific interface on top of an actual container.
You wouldn't want your stack to have an operator
so we wrap std::deque
(by default, you could use a different container) so we don't expose operations a stack wouldn't have.
edited Nov 9 at 14:25
answered Nov 9 at 14:18
NathanOliver
85.5k15118177
85.5k15118177
add a comment |
add a comment |
up vote
3
down vote
std::stack
and std::queue
are so-called "container adapters", as in they adapt the interface of the wrapped container (a template parameter, defaulted to std::deque
, but could be anything that implements back()
, push_back()
and pop_back()
for std::stack
, and back()
, pop_front()
and push_back()
for std::queue
) to the restricted interface of a stack or a queue; if you prefer, they dumb down the underlying container interface.
The general idea should be that the "restricted" container provides just the operations of the "expected mode of operation" of the container - an std::stack
allows only stack-like operations, unlike the underlying std::deque
that allows, for example, random insert/access. Another advantage is that compile-time polymorphism over the underlying container type is a little more comfortable than passing the plain container (you may get better error messages).
In practice, I always found these adapters at least useless, more often just hindrances:
- they don't restrict the space of available states, as, given extra space, both an
std::stack
andstd::queue
can be mutated in any way; - they don't provide type erasure over the underlying container type, as it's a template parameter, so they aren't useful to write a non-template function that takes any stack, regardless of the underlying container;
- most often their interface is way too restricted; you cannot even do a debug print of the full content of an
std::stack
, even if the underlying container fully supported forward iterators (or even random access).
Your second bullet point is easy to overcome by adding an extra template parameter to the function: coliru.stacked-crooked.com/a/79028b2d002e58fe. The other points though are very valid.
– NathanOliver
Nov 9 at 14:35
@NathanOliver: that's exactly the case I mention in my second paragraph, about compile-time polymorphism. But that's not the same as "regular" polymorphism, as it infects everything with templates, and the only advantage it provides over having the function accepting a generic container is that it provides better error messages.
– Matteo Italia
Nov 9 at 14:37
add a comment |
up vote
3
down vote
std::stack
and std::queue
are so-called "container adapters", as in they adapt the interface of the wrapped container (a template parameter, defaulted to std::deque
, but could be anything that implements back()
, push_back()
and pop_back()
for std::stack
, and back()
, pop_front()
and push_back()
for std::queue
) to the restricted interface of a stack or a queue; if you prefer, they dumb down the underlying container interface.
The general idea should be that the "restricted" container provides just the operations of the "expected mode of operation" of the container - an std::stack
allows only stack-like operations, unlike the underlying std::deque
that allows, for example, random insert/access. Another advantage is that compile-time polymorphism over the underlying container type is a little more comfortable than passing the plain container (you may get better error messages).
In practice, I always found these adapters at least useless, more often just hindrances:
- they don't restrict the space of available states, as, given extra space, both an
std::stack
andstd::queue
can be mutated in any way; - they don't provide type erasure over the underlying container type, as it's a template parameter, so they aren't useful to write a non-template function that takes any stack, regardless of the underlying container;
- most often their interface is way too restricted; you cannot even do a debug print of the full content of an
std::stack
, even if the underlying container fully supported forward iterators (or even random access).
Your second bullet point is easy to overcome by adding an extra template parameter to the function: coliru.stacked-crooked.com/a/79028b2d002e58fe. The other points though are very valid.
– NathanOliver
Nov 9 at 14:35
@NathanOliver: that's exactly the case I mention in my second paragraph, about compile-time polymorphism. But that's not the same as "regular" polymorphism, as it infects everything with templates, and the only advantage it provides over having the function accepting a generic container is that it provides better error messages.
– Matteo Italia
Nov 9 at 14:37
add a comment |
up vote
3
down vote
up vote
3
down vote
std::stack
and std::queue
are so-called "container adapters", as in they adapt the interface of the wrapped container (a template parameter, defaulted to std::deque
, but could be anything that implements back()
, push_back()
and pop_back()
for std::stack
, and back()
, pop_front()
and push_back()
for std::queue
) to the restricted interface of a stack or a queue; if you prefer, they dumb down the underlying container interface.
The general idea should be that the "restricted" container provides just the operations of the "expected mode of operation" of the container - an std::stack
allows only stack-like operations, unlike the underlying std::deque
that allows, for example, random insert/access. Another advantage is that compile-time polymorphism over the underlying container type is a little more comfortable than passing the plain container (you may get better error messages).
In practice, I always found these adapters at least useless, more often just hindrances:
- they don't restrict the space of available states, as, given extra space, both an
std::stack
andstd::queue
can be mutated in any way; - they don't provide type erasure over the underlying container type, as it's a template parameter, so they aren't useful to write a non-template function that takes any stack, regardless of the underlying container;
- most often their interface is way too restricted; you cannot even do a debug print of the full content of an
std::stack
, even if the underlying container fully supported forward iterators (or even random access).
std::stack
and std::queue
are so-called "container adapters", as in they adapt the interface of the wrapped container (a template parameter, defaulted to std::deque
, but could be anything that implements back()
, push_back()
and pop_back()
for std::stack
, and back()
, pop_front()
and push_back()
for std::queue
) to the restricted interface of a stack or a queue; if you prefer, they dumb down the underlying container interface.
The general idea should be that the "restricted" container provides just the operations of the "expected mode of operation" of the container - an std::stack
allows only stack-like operations, unlike the underlying std::deque
that allows, for example, random insert/access. Another advantage is that compile-time polymorphism over the underlying container type is a little more comfortable than passing the plain container (you may get better error messages).
In practice, I always found these adapters at least useless, more often just hindrances:
- they don't restrict the space of available states, as, given extra space, both an
std::stack
andstd::queue
can be mutated in any way; - they don't provide type erasure over the underlying container type, as it's a template parameter, so they aren't useful to write a non-template function that takes any stack, regardless of the underlying container;
- most often their interface is way too restricted; you cannot even do a debug print of the full content of an
std::stack
, even if the underlying container fully supported forward iterators (or even random access).
answered Nov 9 at 14:28
Matteo Italia
97.7k13137236
97.7k13137236
Your second bullet point is easy to overcome by adding an extra template parameter to the function: coliru.stacked-crooked.com/a/79028b2d002e58fe. The other points though are very valid.
– NathanOliver
Nov 9 at 14:35
@NathanOliver: that's exactly the case I mention in my second paragraph, about compile-time polymorphism. But that's not the same as "regular" polymorphism, as it infects everything with templates, and the only advantage it provides over having the function accepting a generic container is that it provides better error messages.
– Matteo Italia
Nov 9 at 14:37
add a comment |
Your second bullet point is easy to overcome by adding an extra template parameter to the function: coliru.stacked-crooked.com/a/79028b2d002e58fe. The other points though are very valid.
– NathanOliver
Nov 9 at 14:35
@NathanOliver: that's exactly the case I mention in my second paragraph, about compile-time polymorphism. But that's not the same as "regular" polymorphism, as it infects everything with templates, and the only advantage it provides over having the function accepting a generic container is that it provides better error messages.
– Matteo Italia
Nov 9 at 14:37
Your second bullet point is easy to overcome by adding an extra template parameter to the function: coliru.stacked-crooked.com/a/79028b2d002e58fe. The other points though are very valid.
– NathanOliver
Nov 9 at 14:35
Your second bullet point is easy to overcome by adding an extra template parameter to the function: coliru.stacked-crooked.com/a/79028b2d002e58fe. The other points though are very valid.
– NathanOliver
Nov 9 at 14:35
@NathanOliver: that's exactly the case I mention in my second paragraph, about compile-time polymorphism. But that's not the same as "regular" polymorphism, as it infects everything with templates, and the only advantage it provides over having the function accepting a generic container is that it provides better error messages.
– Matteo Italia
Nov 9 at 14:37
@NathanOliver: that's exactly the case I mention in my second paragraph, about compile-time polymorphism. But that's not the same as "regular" polymorphism, as it infects everything with templates, and the only advantage it provides over having the function accepting a generic container is that it provides better error messages.
– Matteo Italia
Nov 9 at 14:37
add a comment |
up vote
1
down vote
Both, std::stack
and std::queue
are container adaptors. The use a std::deque
as the backing container by default, but thats not necessarily the case.
Both, stack and queue, have a reduced interface compared to a deque. Hence, you might want to have a stack that does not use a std::deque
but some other container and this is what the adaptors are for.
add a comment |
up vote
1
down vote
Both, std::stack
and std::queue
are container adaptors. The use a std::deque
as the backing container by default, but thats not necessarily the case.
Both, stack and queue, have a reduced interface compared to a deque. Hence, you might want to have a stack that does not use a std::deque
but some other container and this is what the adaptors are for.
add a comment |
up vote
1
down vote
up vote
1
down vote
Both, std::stack
and std::queue
are container adaptors. The use a std::deque
as the backing container by default, but thats not necessarily the case.
Both, stack and queue, have a reduced interface compared to a deque. Hence, you might want to have a stack that does not use a std::deque
but some other container and this is what the adaptors are for.
Both, std::stack
and std::queue
are container adaptors. The use a std::deque
as the backing container by default, but thats not necessarily the case.
Both, stack and queue, have a reduced interface compared to a deque. Hence, you might want to have a stack that does not use a std::deque
but some other container and this is what the adaptors are for.
answered Nov 9 at 14:17
user463035818
16.4k42663
16.4k42663
add a comment |
add a comment |
1
They're not mutually exclusive. In fact, both
std::stack
andstd::queue
usestd::deque
by default.– chris
Nov 9 at 14:12
stackoverflow.com/questions/2247982/…
– Mat
Nov 9 at 14:15
1
Sometimes less is more. It's much easier to understand the role in the program of a stack than of a deque that's being used as if it were a stack.
– Pete Becker
Nov 9 at 14:17
1
A type's "power" is often synonymous with risk of use. More "powerful" features make it harder to detect errors since by definition there are fewer things you aren't allowed to do. A prime example of this is c style casts. They are immensely powerful in what casts they can perform they may replace any of the c++ style casts. But their power also means they are dangerous to use. So despite the fact that they make all other casts redundant, their use is discouraged specifically because it's so powerful. Rather than viewing
queue
andstack
being "less powerful", look at them as being safer.– François Andrieux
Nov 9 at 14:23
2
@FrançoisAndrieux nicely put, i'd add that using
queue
andstack
is more expressive. Code is much easier to reason about when a stack is astack
and a queue is aqueue
– user463035818
Nov 9 at 15:20