On Oct 2, 10:25 am, Erik Wikström <Erik-wikst...@telia.com> wrote:
> On 2007-10-02 09:31, t wrote:
> > The stack container adaptor uses deque as its default underlying
> > sequential container. I don't understand why since we only add
> > elements and remove elements from one side of a stack. Why isn't the
> > default vector instead? For that matter, why not list since we don't
> > access elements in the middle of the stack? I don't know if it's
> > necessary (and don't see that it is) to do so in the implementation of
> > stack.
> Yes, vector, list, and deque can all be used. Why deque was chosen as
> default and not vector I do not know, but perhaps it have better
> performance when performing lots of push_back and pop_back operations.
As Kai-Uwe pointed out, it will probably involve less copying on
the average. Although it all depends---if you're constantly
pushing and popping, presumably, the vector will reach its
maximum size fairly quickly, and after that, there will be only
one copy per push, just as with deque. (In general, however, I
think that deque is conceived as being "optimal" for pushing and
popping, even at the end, and vector is conceived as being
"optimal" for random access.)
> > Separate question: vector cannot be used for queue adaptor
> > because it doesn't have push_front. (Lippman C++ Primer,
> > 4th ed, p350). Why would queue need push_front when we only
> > add to the back of the queue?
> Actually, from what I can see from the standard, Lippman is
> wrong. What vector lack is the pop_front method, and just like
> you said, push_back is used to add elements.
Of course, push_front and pop_front go together. If a container
implements one, it will implement the other.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34